Tuesday, June 14, 2016

Life, the Universe, and Everything

Problem

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... 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.


Sample Input

1
2
88
42
99
Sample Output
1
2
88
Solution
C
#include <stdio.h> 
int main()
{
    int input;
    while(1)
    {
        scanf("%d",&input);
        if(input!=42)
        printf("%d\n",input);
        else
        break;
    }
}
C++
#include <iostream>
using namespace std; 
int main()
{
    int n;
    while(1)
    {
        cin>>n;
        if(n==42)
        break;

        cout<<n<<endl;
    }
    return 0;
}
Python
while(True):
    no = int(raw_input())
    if no == 42:
        break
    else :
        print no
Java
import java.util.*;
class TestClass {

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

4 comments:

  1. where do you place the numbers input and ouput to test?

    ReplyDelete
  2. The code in c is not working sir,display is not coming

    ReplyDelete
  3. The program does not have input limits so an error occurred

    ReplyDelete
  4. #include
    using namespace std;
    class node
    {
    public:
    int data;
    node* next;
    };
    int main()
    {
    node* head = new node;
    node* t;
    node* last;
    int x;
    cin >> x;
    head->data = x;
    head->next = 0;
    last = head;
    cin >> x;
    while (x != 42)
    {
    t = new node;
    t->data = x;
    t->next = 0;
    last->next = t;
    last = t;
    cin >> x;
    }
    node* p = head;
    while (p != 0)
    {
    cout << p->data << endl;
    p = p->next;
    }
    }

    ReplyDelete