Write a program to print 5 random numbers between 1 and 100
Answers
Answered by
18
Required Answer:-
Question:
- Write a program to print 5 random numbers between 1 and 100.
Solution:
Here comes the program.
1. In Java.
public class Random {
public static void main(String[] args) {
for(int i=1;i<=5;i++) {
int d=1+(int)(Math.random()*100);
System.out.print(d+" ");
}
}
}
2. In Python.
import random
for i in range(5):
print(random.randínt(1,100),end=" ")
Explanation:
- Math.random() function in Java is used to display random numbers. To display random numbers from 1 to 100, the syntax is - 1 + (int)(Math.random() * 100)
- To display random numbers in Python from m to n, syntax is - random.randînt(m, n);
- Using random function, the problem is solved easily.
Similar questions