21 / 92
May 2011

If you redirect your stdout to a file you will realize that you don't get the same answer. Try it out on ideone.com.

thanx for the quick answer! but how exactly could i redirect my output to a file? as i said im really kinda nooby in programming, dont even know how to use 'for' loops yet, but i would really like to solve this myself, before i move on to the next problem

1 month later

Hi
Am new to programming world and this is my first attempt.
Could some one tell me what mistake am making in this pgm?

include

int i, num[5];

int main()
{
for (i=0;i<5;i++)
{
scanf("%d",&num[i]);
}
for (i=0;i<5 && num[i]!=42;i++)
{
printf("%d\n",num[i]);
}
return 0;
}

This works fine in terminal but not a right ans in SPOJ.

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.