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
Write a program by using scanner class to input a sentence. Print each word of the sentence along with the sum of the ASCII codes of its characters.
Write a program by using scanner class to accept a set of positive and negative numbers randomly. Print all the negative numbers first and then all the positive numbers without changing the order of the numbers.
Consider a String:
THE COLD WATER AND THE HOT WATER GLASSES ARE KEPT ON THE TABLEWrite a program by using scanner class to enter the string and display the new string after removing repeating token 'THE'. The new string is:
COLD WATER AND HOT WATER GLASSES ARE KEPT ON TABLE
Using scanner class, write a program to input a string and display all the tokens of the string which begin with a capital letter and end with a small letter.
Sample Input: The capital of India is New Delhi
Sample Output: The India New Delhi