TEST - Editorial

int main()

{

int i;
do
{
cout << "Enter i : ";
cin >> i;
if (i==42 && i<100 && i>-100)
{
break;
}
else
{
cout << i;
}
} while (i != 42 && i<100 && i>-100);
return 0;
}

#include<stdio.h>

void main()
{
int i,n,a[20];

printf("\nenter total noā€™s :");
scanf("%d",&n);
printf("\nenter some input noā€™s :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
i=0;
while(a[i]!=ā€™/0ā€™)
{
if(a[i]==42)
{break;
exit(1);}
else
{
printf("%d",a[i]);
}
}
}

spoj problem :slight_smile:

#include
using namespace std;

int main()
{
int n=0;
while(n>=0)
{

  cin>>n;
if(n>99)
break;
  else {

  if(n!=42)
  {
 cout<<n<<endl;


}
 else
  break;

  }
 }


   return 0;
  }

//Code by swapnil1796
#include<stdio.h>
void main(){
int a;
while(scanf("%d",&a) && a!=42)
{
printf("%d\n",a);
}
}

Runtime error. Please explain

import java.util.Scanner;
class First{
public static void main(){
Scanner sc=new Scanner(int);
int a=0;
while(a!=42){

int a=sc.nextInt();}}

i am using this code in c language but itā€™s giving runtime error ,can you please help?
#include<stdio.h>

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

}

Donā€™t give statements like ā€˜Enter numberā€™ etc. Directly take input and print output in the given format.

@premang9270 you are not supposed to print things like "Enter i : " or something which is not specified in the problems statement.

The correct version of your code is ::

do {
    cin >> i;
    if(i == 42) {
        break;
    }
    else {
        cout << i;
    }
}while(i != 42);

**NOTE :: ** you are not given that the numbers lie in b/w -100 to 100 so donā€™t use the condition i<100 && i>-100.

read the last line of the question , that says digits must of two or one digits

1 Like

Yeah you are right still you donā€™t need that condition to be checked.

bro, the solutions are tested by an online judge. You should strictly obey the input/output format. For your case you are asking for total no of inputs and enter some new numbers these are unnecessary. just do an infinite loop beak it when u encounter 42.(for input output format codechef is providing sample cases below the problem) Happy codingā€¦

#include<stdio.h>
int main()
{
while(1)
{
int a;
scanf("%d",&a);
if(a<=99&&a>=-99&&a!=42)
printf("%d\n", a);
else
break;
}
return 0;
}

infinite loop

this is my code in c its showing wrong after submission even if custom input and output are correct

#include <stdio.h>

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

return 0;

}

You are only running for 100 cases. You have to run till you get 42

Thank you for replying sir/madam ,
But i got the custom input and output as they mentioned and it is running till i get 42 and as if it encounter 42 it just come out of the loop as i placed a break statement in the loop

For example take there were 200 inputs and 200th number was 42. Then your code will give WA. Simply run an infinite loop and if 42 is found then break it

/* package codechef; // donā€™t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be ā€œMainā€ only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);

	while(true)
	{
	    int n=sc.nextInt();
	    if(n!=42)
	        System.out.println(n);
	    else break;
	}
}

}

What is wrong in this code, iā€™m getting an error NZEC no such element exception . Help me out please.

int main() {
int a = 0;

while(a<100 && a!=42){
    scanf("%d", &a);
    if(a!=42){
        printf("%d\n", a);
    }
}
return 0;

}