Just thought I'd share mine since I haven't seen it in this topic yet.
#include <stdio.h>
int main(void) {
char x[3];
while(atoi(gets(x))!=42)puts(x);
return 0;
}
Alternatively with fgets() and fputs():
#include <stdio.h>
int main(void)
{
char x[4];
while (atoi(fgets(x,4,stdin))!=42)
{
fputs(x,stdout);
}
return 0;
}
@leppy, is it the size of the character array? I believe I made the size of my array 3 in my original version, but I was just playing around to see if I could get away with buffer overflow. ^^
Sorry I forgot to change it back. I edited the post to make the size of the array 3, which should account for the max 2 characters per line (not counting newline).
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int arr[10000];
int count=0,i=0,flag=1;
printf("Enter Input:");
while(flag)
{
scanf("%d",&arr[i]);
if(arr[i]==42)
flag=0;
else
flag=1;
i++;
count++;
}
for(i=0;i<count-1;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
can you plz explain me what is wrong with this code because it gives the same output that is required but i do no understand they considered it as wrong answer.
Every one is writing code only for the above input only.
It should not be like that na.
We have to sort all the numbers before 42 in ascending order (as mentioned in the question...."More precisely... rewrite small numbers from input to output").
Then we have to print all the numbers.
why everyone is writing the code without sorting?
or
Any problem with my way of understanding the question(let me know the correct way)??
and
why admin did consider the two digit input case as the program can accept any number na..????
Thanks in advance
There is nothing in this problem statement that says to sort the numbers.
"More precisely..." means that the statement to follow will make the previous statement more precise.
"rewrite small numbers from input to output"
Input is where the numbers are read from. Output is where the numbers get printed to. Rewrite means to copy. The problem is asking you to read the numbers from input and write them to output. Stop when the number is 42.
Keeping the numbers one or two digits keeps the problem simple. It also allows for some creative solutions.
If you need more clarification, please post again.
i wrote the following code
and in my terminal im getting the desired output but for some reason in the SPOJ its showing wrong answer
please help !! im net to SPOJ;
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
scanf("%d",&a);
while(a>0&&a!=42)
{
printf("%d",a);
scanf("%d",&a);
}
return 0;
}