You are not logged in. Please login at www.codechef.com to post your questions!

×

TEST - Editorial

PROBLEM LINK:

Practice

Author: ADMIN

Editorialist: SUSHANT AGARWAL

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Basic looping,Basic Input/Output

PROBLEM:

Rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

EXPLANATION:

Please refer to the sample solution given by editorialist.

EDITORIALIST'S SOLUTION:

Editorialist's solution can be found here.

This question is marked "community wiki".

asked 17 Dec '14, 14:23

sushant96's gravatar image

0★sushant96
21368
accept rate: 0%

edited 02 Jan '15, 17:25

admin's gravatar image

0★admin ♦♦
17.4k347486515


#include<iostream.h>

void main()
{
 int a,flag=0;
 do
 { 
  cin>>a;
  if(a<=99 && a>=(-99) && a!=42)
  {
   flag=1;
   cout<<a;
  }
 }while(flag==1); 
}
link

answered 02 Jan '15, 18:07

robo_kingg_01's gravatar image

0★robo_kingg_01
1
accept rate: 0%

edited 02 Jan '15, 19:36

betlista's gravatar image

3★betlista ♦♦
16.8k49115225

import java.util.*; public class inToOut{

public static void main(String [] args)
{
    Scanner in = new Scanner(System.in);
    while(in.hasNextInt() )
    {
        int temp = in.nextInt();
        if(temp == 42)
            break;
        System.out.println(temp);
    }

}

}

link

answered 23 Jan '15, 16:46

lkalig2's gravatar image

0★lkalig2
1
accept rate: 0%

include<stdio.h>

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

link

answered 27 Jan '15, 10:21

ashish66's gravatar image

1★ashish66
1
accept rate: 0%

edited 27 Jan '15, 10:28

include<stdio.h>

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

link

answered 01 Feb '15, 23:33

sonalisahu64's gravatar image

0★sonalisahu64
1
accept rate: 0%

edited 02 Feb '15, 00:13

include<stdio.h>

include<conio.h>

void main() { int x; printf("\n Enter a two digit no to stop enter 42 "); scanf("%d",&x); if((x>99)||(x=42)) break; else printf("the entered no is : %d",x); }

link

answered 11 Feb '15, 19:32

sanjana12's gravatar image

0★sanjana12
1
accept rate: 0%

Don't give statements like 'Enter number' etc. Directly take input and print output in the given format.

(11 Feb '15, 21:18) drgn_hart3★

include<stdio.h>

main() { int num=0; while(num<100) { scanf("%d",&num); if(num!=42) printf("%d",num); else continue; }

link

answered 08 Mar '15, 23:27

zufrieden's gravatar image

2★zufrieden
1
accept rate: 0%

/this is corect answer even tho sowing false/

include <stdio.h>

int main () {
int n;
for (;n!=42;)
{
scanf ("%d",&n);

printf ("%d\n",n);} return 0; }

link

answered 13 Mar '15, 22:47

prerak13's gravatar image

0★prerak13
1
accept rate: 0%

import java.util.Scanner;

public class LifeTheUniverseAndEverything {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    while (true) {
        int i = scanner.nextInt();
        if (i == 42)
            break;
        System.out.println(i);
    }

}

}

link

answered 23 Mar '15, 14:30

vipulchauhan's gravatar image

0★vipulchauhan
1
accept rate: 0%

include<stdio.h>

int main(void) { unsigned int num; for(;printf("%d",scanf("%d",&num)!=42);); return(0); }

link

answered 04 Apr '15, 20:34

prash_7's gravatar image

0★prash_7
1
accept rate: 0%

import java.util.Scanner;

/ * @author Avi / public class New {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    int poda[];
    poda = new int[10];
    Scanner input = new Scanner(System.in);
    System.out.println("enter the array");
    for (int i =0;i<5;i++)
    {
        poda[i]=input.nextInt();                    
    }

    System.out.printf("the output values are ");
     for (int i =0;i<5;i++)
     {
     if(poda[i]==42)
        {
            break;
        }
         System.out.println(poda[i]);
     }
}

}

link

answered 02 May '15, 15:14

avinashv37's gravatar image

0★avinashv37
1
accept rate: 0%

void main() { int i,a[6]; a[6]={1,42,84,7,8,9}; for(i=0;i<6;i++) { if(a[i]<=a[i+1]) {printf("%d",a[i]); printf("\n"); }else break;

} getch(); }

link

answered 13 May '15, 13:38

bhakudhaked's gravatar image

0★bhakudhaked
1
accept rate: 0%

include<iostream.h>

using namespace std; main() { int n; cout<<"enter a number"; cin>>n; while(n>-99 && n<99 && n!=42) { cout<<"enter a no." cin>>n; cout>>n; }

link

answered 12 Jun '15, 22:51

kumari_puja's gravatar image

0★kumari_puja
1
accept rate: 0%

include<stdio.h>

include<conio.h>

void main() { int n; while(1) { printf("Enter the number:"); scanf("%d",&n); if(n==42) break; printf("%d",&n); } }

link

answered 13 Jul '15, 18:35

hbl_8212's gravatar image

0★hbl_8212
1
accept rate: 0%

my program is having a compile error:

package codechef;

import java.util.Scanner;

public class chef {

private static Scanner input;

public static void main(String[] args) {
    input = new Scanner(System.in);
    int value = 0 ;

    do{
        System.out.println("Enter a number: ");
        value = input.nextInt();
        System.out.println(value);
    }
    while(value != 42);
    System.out.println("Program terminated");
}

}

link

answered 29 Nov '15, 02:34

siddattri's gravatar image

0★siddattri
1
accept rate: 0%

package package1;

import java.util.Scanner;

public class Class1 {

public static void main(String[] args) throws Exception {

    while(true)

            {


                System.out.println("Enter an integer");
                Scanner sc = new Scanner(System.in);
                int num = sc.nextInt();

                if( (num<100) && (num>(-100)) && (num!=42) )                
                       System.out.println(num);
                else
                    return;
            }
}

}

link

answered 18 Dec '15, 15:05

chikku_123's gravatar image

0★chikku_123
1
accept rate: 0%

include <iostream>

using namespace std;

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;

}

link

answered 22 Dec '16, 12:04

premang9270's gravatar image

0★premang9270
1
accept rate: 0%

@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.

(22 Dec '16, 12:15) coder_voder2★
2

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

(22 Dec '16, 16:39) premang92700★

Yeah you are right still you don't need that condition to be checked.

(22 Dec '16, 17:19) coder_voder2★

include< iostream >

include< conio.h >

using namespace std; int main() { int a[1000],i; cout <<"enter integers" << endl; for( i=1 ; i<=1000 ; i++ ) { cin >> a[i]; if ( a[i]==42 ) { break; } } getch (); return 0; }

link

answered 09 Mar, 12:35

gagan22g's gravatar image

0★gagan22g
1
accept rate: 0%

edited 09 Mar, 12:38

include<stdio.h>

main() { int a[100],i; printf("enter numbers and enter 42 to end"); for(i=0;i<100;i++) {scanf("%d",&a[i]);if(a[i]==42)break;else printf("%d",a[i]);} }

link

answered 13 May, 22:08

vaishnavi6799's gravatar image

0★vaishnavi6799
1
accept rate: 0%

//C Solution by satadru97
    #include<stdio.h>
    int main(void)
    {
        int n;
        while(1)
        {
            scanf("%d",&n);
            if(n!=42)
                printf("%d\n",n);
            else
                break;
        }
        return 0;
    }
link
This answer is marked "community wiki".

answered 10 Jun, 08:54

satadru97's gravatar image

1★satadru97
1
accept rate: 0%

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

link

answered 16 Jun, 16:03

matta's gravatar image

0★matta
1
accept rate: 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]); } } }

link

answered 05 Jul, 13:55

jenettejoy05's gravatar image

0★jenettejoy05
1
accept rate: 0%

edited 05 Jul, 14:03

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..

(05 Jul, 14:18) abhikalpu_1233★
Answer is hidden as author is suspended. Click here to view.

answered 05 Jul, 15:02

raj79's gravatar image

4★raj79
(suspended)
accept rate: 11%

#include <iostream>
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;
  }
link

answered 05 Jul, 17:58

manish684's gravatar image

2★manish684
1
accept rate: 0%

edited 05 Jul, 18:02

//Code by swapnil1796

include<stdio.h>

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

Runtime error. Please explain

link

answered 05 Sep, 20:58

swapnil1796's gravatar image

0★swapnil1796
1
accept rate: 0%

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

link

answered 05 Oct, 17:54

aishwaryagupta's gravatar image

0★aishwaryagupta
1
accept rate: 0%

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

}

link

answered 12 Oct, 22:59

subhashis97's gravatar image

0★subhashis97
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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

Question tags:

×1,147
×143
×22

question asked: 17 Dec '14, 14:23

question was seen: 8,732 times

last updated: 12 Oct, 22:59