Life,The universe and everything

,

What is wrong with the following code. It shows wrong answer in codechef.

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

int main()
{
int a;

while(a!=42)
{
    scanf("%d",&a);
     if(a<0&&a>100)

            printf("%d",a);

}

return 0;}

The line:
if (a < 0 && a > 100) will never be true, since a number can never simultaneously be both less than 0 and greater than 100.

Further, the input specification clearly mentions that all the numbers are strictly 2-digit numbers, and hence you need not attempt to verify this within your program.

Also, initialize your value a, since at the line while (a != 42), for the first iteration, a is undefined and can have an unpredictable outcome.

To summarize:

  • Remove the “if” condition
  • Initialize a at the time of declaration, say to -1.

Taking care of the above two points should solve the problem. :slight_smile:

Could anyone please explain me what is wrong with my code
#include<stdio.h>
int main()
{
int n,i,a[1000];
for(i=0;i<n;i++)
{
{
scanf("%d",&a[i]);
if(a[i]==42)
{
` exit(0);
}printf("%d\n",a[i]);
}
}
}

There is no n in the question. You have to keep taking the input from the user, until you encounter a 42, which is when you break. Also, it is possible that there are more than 1000 numbers, so storing them in a 1000 sized array will give a segmentation fault.

#include
using namespace std;

int main()
{
int len=5;
int a[len],i,j;
for(i=0;i<len;i++)
{
cin>>a[i];

}

for(j=0; j<len; j++)
{
    if(a[j]!=42)
	{
		cout<<a[j]<<"\t";
	}     
	else
    {
        break;
    }
    
}

return 0;

}

is it correct??

How about this

#include

using namespace std;

int main()
{
int n;

while(cin>>n)

    { 

    if(n==42)break;


	else

	cout<<n<<endl;
}

}

#include<stdio.h>
int main()
{
int i,a[5];
printf(“enter the nos”);
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++)
{
if(a[i]!=42)
{
printf("%d",a[i]);
}
else
{
break;
}
}
return 0;
}

What is wrong with this code?
#include<stdio.h>
int main()
{
int i=0,j=0,storage[]={0};
//while(j>=0 && j<100)
while(j==0)
{
//j++;
scanf("%d",&storage[i]);
printf(“i=%d\n”,i);
if(storage[i]!=42)
printf(“arr = %d\n”,storage[i]);
else
break;
}
return 0;
}

Plus, use “\n” after printing your value in a otherwise output will not be displayed on the new line.

#include <stdio.h>
int main(void)
{
int i;
while (1) {
scanf("%d", &i);
if (i > 99)
continue;
if (i == 42)
break;
printf("%d", i);
}
return 0;
}

So,what exactly is wrong with my code?

Firstly u should initialise n and in printf \n escape sequence should be there. Please upvote if it eas useful. Thank u

You must read until you reach the End of File (EOF), because there is an arbitrary amount of numbers in the input.

You must read until you reach the End of File (EOF), because there is an arbitrary amount of numbers in the input.