30 / 92
Jun 2012

The can be many more than 5 numbers in the input. Read number. If it's 42, stop. If it's not 42, print it. Repeat.

5 months later

include

define SIZE 10

int main()
{
int a[SIZE],i,j;
printf("INPUT:\n");
for(i=0;i {
scanf("%d\n",&a[i]);
if(a[i]>99)
{
scanf("%d\n",&a[i]);
}
if(a[i]==42)
{
printf("OUTPUT:\n");
for(j=0;j {
printf("%d\n",a[j]);
}
}
}
return 0;
}

3 months later

dis is my code:

include

int main()
{
int a[5];
int i;
printf("Enter 5 2digit numbers:\t");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Output:\n");
for(i=0;a[i]!=42&&a[i]!=NULL&&i<5;i++)
{
printf("%d\n",a[i]);
}
return 0;
}

still i am getting wrong output as an answer...plz help

The judge considers everything that you output. Therefore you should not be prompting for input.

Also, read the problem statement. Where does it say that there will be exactly 5 numbers in the input?

2 months later

can any geek tell me why the following code is giving a wrong answer.......

include

int main()
{
int x,k=0;

scanf("%d",&x);
while(x>0)
{
if( x==42)
k=1;
if(k==0 && x<100)
printf("%d\n",x);
scanf("%d",&x);
}
return 0;
}

4 months later

You are assuming that the file ends in '\n'. In reality you should not care how the file ends. You should ony care about finding "42". Use scanf.

7 months later

i am running this code and it is getting me a run time error.. why ?> plz help me. i m new to programming

include

int main()
{
int num;
scanf("%d", &num);
while(num!=42)
{
printf("%d\n", num);
scanf("%d", &num);
}
}

Please use code tags when posting code.

You always need to return 0; from main in C.

5 months later

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).

2 months later

include

int main(){
int i,x,c;
int a[100];
i=0;

	while(x>=0 && x<100 && i<100)
	{ 
		scanf("%d",&x);
		a[i]=x;
		i++;

	}
	c=i;	

for(i=0;i { if(a[i]!=42)
printf("%d\n",a[i]);
else
break;
}

return 0;
}

whats the error

15 days later

#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.

25 days later

The judge considers all output for the solution.

    printf("Enter Input:");

As soon as you print this line the judge has received the wrong answer.

Please post back if you have more questions.

3 months later

output is correct but it was showing run-time error

include

void main()
{
int j,i;
for(i=0;i<100;i++)
{
scanf("%d",&j);
if(j==42) break;
printf("%d",j);
}
}

Please use code tags when posting code.

For the input:

10
23
42
3
10

Your code returns:

1023

It should return:

10
23

Why are you using 100? There is no indication as to how many numbers are in the input.

2 months later

Why i am getting compilation error??
[code]

include

include

int main()
{
int n;
while(1)
{
scanf("%d",&n);
if(n>=0&&n<100&&n!=42)

        printf("%d\n",n);
    else
        exit(0);

}

return 0;

}

If you click on where it says "Compilation Error" it will actually list you the compilation error.