Write a program named program52.py that uses main and a void function named numb
ID: 3938160 • Letter: W
Question
Write a program named program52.py that uses main and a void function named numbers that takes no arguments and does not return anything. The numbers function generates 5 random integers, each greater than 10 and less than 30 (duplicates are okay), and prints them all on one line separated by spaces. A loop is required for this, and the loop should also total up the integers so the sum can be displayed when the loop ends. The main function should call the numbers function.
SAMPLE OUTPUT
12 24 16 21 17
The total is 90
Explanation / Answer
import java.util.Random;
class program52y
{
static void numbers()
{
int Low = 11;
int High = 29;
int Result,total=0;
Random r = new Random();
for(int i=0;i<5;i++)
{
Result = r.nextInt(High-Low) + Low;
System.out.print(" "+Result);
total=total+Result;
}
System.out.println();
System.out.println("The total is "+total);
}
public static void main (String args[])
{
numbers();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.