Tuesday, 19 August 2014

Random Number Generator

//Program for generating Random number

import java.io.*;
import java.util.Random;
class Random_number
{
    public static void main(String s[]) throws Exception
    {
        int n;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter number between 1 to 10: ");
        n = Integer.parseInt(br.readLine());

        Random random_num = new Random();
        int r = random_num.nextInt(10);   //generating Random number

        System.out.print("\nRandom number: "+r);

        if(n == r)
        {
            System.out.print("\nYou won...");
        }
        else
        {
            System.out.print("\nYou lose!!!");
        }

        System.out.println();
    }
}
OUTPUT 1:-

Enter number between 1 to 10: 5

Random number: 5
You won...

OUTPUT 2:-

Enter number between 1 to 10: 2

Random number: 8
You lose!!!

No comments:

Post a Comment