#include <stdio.h>
int main(void) {
int x;
for(; scanf("%d",&x) > 0 && x != 42; printf("%d\n", x));
return 0;
}
created
last reply
- 91
replies
- 9.0k
views
- 50
users
- 7
likes
- 5
links
There are 91 replies with an estimated read time of 9 minutes.
#include <stdio.h>
int main(void) {
int x;
for(; scanf("%d",&x) > 0 && x != 42; printf("%d\n", x));
return 0;
}
There are 91 replies with an estimated read time of 9 minutes.
the output of the TEST using C doesnt conform to the output mentioned in the Question ! on the other hand this solves the problem to a far greater extent
#include<stdio.h>
#include<conio.h>
int main()
{
char num[2]={-1,-1};
while(1)
{
if((num[0]=='4')&&(num[1]=='2'))
continue;
else
{
num[0]=getch();
num[1]=getch();
if((num[0]=='4')&&(num[1]=='2'))
continue;
printf("%c%c\n",num[0],num[1]);
}
}
return 0;
}
With all due respect to you and the admin noix for your experience !!
but on compiling the code given by noix this is the output i got for the sample input
1
1
2
2
88
88
and my code gives the output as required !!
and i did not get why are you not getting the answer with my code could you please be more precise.
IT is just the nature of scanf() in noix's code that it echoes the input as it is typed.
getch() does not do that !
scanf does not echo the input as it is typed. The only reason you may see your input on the screen is because you are typing it in - if you redirect the input from a file, it won't echo the input at all.
Your program is in an infinite loop and never ends. It also doesn't handle anything after a 2 digit number, since the newline is read as the first character of the next line. In fact, I don't think getch even works at all in this type of problem.
Thanks, about the help with scanf ! and i corrected my infinite looping code.
but the question asks only for 2digit numbers so why would we need the user/judge to enter more ?
ALSO is there a method to know whether a function echoes input or not ? because it is displayed on the screen so scanf gives an impression that it echoes.[/quote]
Redirect your output to a file rather than the screen, thats the best way.
PS - when I said doesn't handle anything after 2 digit numbers, I mean:
12
34
first you read num[0]=1, num[1]=2, then you read num[0]=newline, num[1]=3, then you read num[0]=4 and num[1]=newline, so you don't get the correct numbers.
Thanks TripleM
well i do understand what you mean by redirecting the output but you mean to say we can use file handling in our programs ! i thought that was not allowed ?
and again is the behavoir of the SPOJ online judge and the ACM UVA judge the same ?
Going back to the problem: can the sample run of the program be like this
1
1
1
2
2
88
88
42
and the program stops after that ?
PS that 42 has been printed.
Hey all.
I am relatively a novice at programming.
I ran the following code in Unix gcc and it worked. But on the site it shows compile error. Please tell me why.
#include<stdio.h>
main ()
{
int a[99],b[99],i,n;
printf("Enter the numbers");
for(i=0;;i++)
{
scanf("%d",&a[i]);
if(a[i]==0)
{
n=i;
break;
}
if(a[i]>99)
{
printf("Incorrect Input");
}
}
for(i=0;i<n;i++)
{
if(a[i]!=42)
{
printf("\n%d\n",a[i]);
}
else
{
printf("\n%d\n",a[i]);
break;
}
}
return 0;
}
Thanks a lot.
Really appreciate the awesome work SPOJ is doing!
Hi all,
I did my solution to this code and it gave me runtime error NZEC. I don't know what the error means. So I started looking in the forum and I saw noix's solution. The thing is that while looking at that solution I realized that it doesn't the read content, this meaning that using stdin and stdout when I insert a number it prints the number back instead of printing it in the end. Of course that, if I redirect the stdin and stdout to files it will give the outputs and inputs correctly.
So my question is (besides that try to solve that error) what is in fact the expected behaviour of this program.
Thank you un advance for your help.
Henrique Vaz
Your code will be tested by redirecting standard input and standard output to/from a file then comparing the correct answer with your answer, so it makes no difference at what point in your program you print out the results, as long as they are printed out. NZEC means different things in different languages, but in C it normally means your main function isn't returning 0 like it always has to.
hello, guys! First of im complete newbie in programming in general and i'm very glad that i found this wonderful site, which really helps me to polish my programming skills!
so i came to this first problem and i tried to submit this code
int main(void) {
int n;
while (1) {
scanf ("%d",&n);
if (n != 42)
printf("%d",n); else break;
}
return (0);
}
it gives me same output as in noix'es solution (i use NetBeans 6.91 IDE), but for some reason SPOJ says wrong answer - im mad confused, any help?