Algorithm


Problem Link - https://www.spoj.com/problems/TEST/ 

TEST - Life, the Universe, and Everything

 

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.

Example

Input:
1
2
88
42
99

Output:
1
2
88

Information

In case of any problems with your code, you can take a look in the forum, you'll find the answer, only for this problem, in various languages.

Code Examples

#1 Code Example with Java Programming

Code - Java Programming

import java.util.*;
class test
{
public static void main(String args[])
{
	int[] current=new int[1000];
	int a=0;
	Scanner sc=new Scanner(System.in);
	try
	{
		for(int i=0;;i++)
		{
			current[i]=sc.nextInt();
			if(current[i]==42)
			{
				a=i;
				break;
			}
		}
		for(int j=0; j<a; j++)
		{
			System.out.println(current[j]);
		}
	}
	catch(Exception e)
	{
		return;
	}
}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
2
88
42
99

Output

x
+
cmd
1
2
88

#2 Code Example with C Programming

Code - C Programming

#include<stdio.h>
int main()
{
    int n;

    while(1)
    {
        scanf("%d", &n);
        if(n==42)
            break;

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

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
2
88
42
99

Output

x
+
cmd
1
2
88

#3 Code Example with C++ Programming

Code - C++ Programming

#include<iostream>
#include<cstdio>

using namespace std;

int main(){
    int n;
    
    while(1){
        scanf("%d",&n);
        if(n==42) break;
        
        printf("%d\n",n);
    }
    
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1
2
88
42
99

Output

x
+
cmd
1
2
88
Advertisements

Demonstration


SPOJ Solution - Test Life, the Universe, and Everything - Solution in C, C++, Java, Python

Next
SPOJ Solution-CHMAZE Changing Maze-Solution in C, C++, Java, Python