I tried and worked one practice problem and it got executed without any errors on Compilr.com which uses gcc 4.6. But when i posted the code in here and when it was tested its throwing up wrong answer everytime. What is the problem? I’m a beginner kindly guide me. I use C language as platform.
I have checked the solution. Same kind of logic has been used. But then i dont find it of any use. Is there anything to do with the compiler versions?? Like 4.3.2 and 4.6??
Hello @brjz007,
This code gets AC and it’s based on your code
#include<stdio.h>
int main(void)
{
int n;
while(1)
{
scanf("%d",&n);
if(n>=0 && n<=99 && n!=42)
{
printf("%d\n",n);
}
else
break;
}
return 0;
}
You were simply forgetting of the = sign on the condition n >= 0 && n <= 99. Changing this gets your solution to be AC. Also, your logic is perfectly correct AND you CAN AND SHOULD ASSUME the values you did for n. (@himanshujaju, read statement before making such claims please )
However, some comments about it:
- You don’t need to write void inside the main brackets;
- You also don’t need while(1)… You can input a 1st number before while loop;
Below is my C implementation for this problem:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
while(n != 42)
{
printf("%d\n",n);
scanf("%d",&n);
}
return 0;
}
Hope I could help,
Bruno