Life, the Universe, and Everything

What is the probem with the following code, somebody please explain. May be I am missing some test case!

#include< stdio.h >
#include< stdlib.h >

int main()

{

int n,i=0,arr[] = {2,43,6,76,87,77,34,23,42,40,6,75,65};

	while (arr[i] || arr[i]==0)
	{
		if (arr[i] == 42)
			exit(0);
		printf("%d\n",arr[i]);
		i++;
	}
	return 0;
}

The question Life,the Universe and Everything says: “rewrite small numbers from input to output. Stop processing input after reading in the number 42”…
Your code doesn’t seem to take any input…

All you need to do is:

  1. repeat steps 2 and 3

  2. Take input as scanf("%d",&a);

  3. If number is 42 break; else printf("%d\n",a);

Hope this helps you understand the problem…

Before I submitted this code but this too gave the wrong answer as per their compiler! What’s wrong with this one as it lets the user to input!

#include< stdio.h >
#include< stdlib.h >

int main()

{

int n;
while (scanf("%d", &n))
{
	if (n == 42)
		exit(0);
	n++;
}
return 0;

}

u also need to print the values…!!!

U need to do both, take the input and output it…as mentioned by kunal361

Then, I think “This” should fix it. I have already given 5 wrong attempts don’t wanna play around with it anymore.

#include< stdio.h >
#include< stdlib.h >
int main()

{

int n;

while (scanf("%d", &n))

{

if (n == 42)
    exit(0);
printf("%d\n",n);     //This
n++;

}

return 0;

}

Compilation error.

i saw ur submission…http://www.codechef.com/viewplaintext/3136264 …where are the hashes before the header files???

1 Like

Updated it as http://www.codechef.com/viewplaintext/3136302 Still compilation error!

there should be no space between the"<" and the header file name…it should be like…"<stdio.h>" and not “< stdio.h >”…!!!

Thanks, It worked! That space was added to add margin when I was posting the question in forum.

for submit the result ,not need to write any header file .codechef compiler also include header file itself.

Thanks for the advice @cooldhanraj :slight_smile:

you need to input the number and output it according to condition! :slight_smile:
hope this helps :slight_smile: