Computer Applications

Write a program to generate random numbers between 1 to N by using scanner class taking N from the console.

Java

Input in Java

6 Likes

Answer

import java.util.Scanner;

public class KboatRandomNumbers
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter N: ");
        int n = in.nextInt();
        int r = (int)(Math.random() * n) + 1;
        System.out.print("Random Number = " + r);
    }
}

Variable Description Table

Program Explanation

Output

Answered By

4 Likes


Related Questions