×

# Life,The universe and everything

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

# include <stdlib.h>

int main() { int a;

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

printf("%d",a);

}


return 0;}

0★zephy
11
accept rate: 0%

1★arun_as
7232618

 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. :) answered 22 Apr '15, 17:36 2★caprico 86●1 accept rate: 25% Plus, use "\n" after printing your value in a otherwise output will not be displayed on the new line. (10 Jan, 23:24)

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]); } } }

0★anil_s_p
1
accept rate: 0%

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

(23 Apr, 05:21)
 0 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. answered 22 Sep '15, 13:10 3★harshvk5 3●1●3 accept rate: 0%

# include<iostream>

using namespace std;

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

}

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

}

return 0;


}

is it correct??

1
accept rate: 0%

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

(23 Apr, 05:23)
 0 How about this #include using namespace std; int main() { int n; `while(cin>>n) { if(n==42)break; else cout<

# 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; }

1
accept rate: 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; }

1
accept rate: 0%

# 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; }

1
accept rate: 0%

 0 So,what exactly is wrong with my code? answered 11 Jan, 22:12 1 accept rate: 0%
 0 Firstly u should initialise n and in printf \n escape sequence should be there. Please upvote if it eas useful. Thank u answered 22 Apr, 23:28 68●2 accept rate: 9%
 toggle preview community wiki
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Tags:

×1,208
×27