Tuesday, June 14, 2016

Factorial!

Problem


You have been given a positive integer N. You need to find and print the Factorial of this number. The Factorial of a positive integer N refers to the product of all number in the range from 1 to N. You can read more about the factorial of a number here.

Input Format:
The first and only line of the input contains a single integer N denoting the number whose factorial you need to find.

Output Format
Output a single line denoting the factorial of the number N.

Sample Input
2

Sample Output
2


Solution

All one needs to do here is to find the product of all the number in the range from 1 to N. This can be done by iterating over the entire range from 1 to N.

import java.io.*;
import java.util.*;
class example_2
{
    static Scanner sc=new Scanner(System.in);

    public static void main(String args[]) throws Exception
    {
        long n=sc.nextLong(),prod=1;
        for(long i=1;i<=n;i++)
        {
            prod=prod*i;
        }
        System.out.println(prod);
    }
}

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