Question

java

1. Write an application that generates n random integers in the range provided the user. Use method genrate Even to generate even numbers only and generateOdd to generate odd numbers void generateOdd(int n, int r1, int r2); void generateEven(int n, int r1, int r2); Sample run: How many integers you have?5 What are your number ranges? 3 10 The generated even numbers are 6 4 8 10 6 The generated odd numbers are 37 9 35

0 0
Add a comment Improve this question Transcribed image text
Answer #1

RandomNumber java-CAUsers\ Shubham Desktop HomeworkLib1 Atom File EditView Selection Find Packages Help RandomNumberjava untitled 1 import java.util. 17 For using inbuilt Scanner class and Random class 2 class RandomNumber public static void main(String args[) main) method 6 Scanner scan- new Scanner(System.in); //Creating the object of Scanner class. 7 8 9 System.out.print(How many integers you have? );//Printing Message to the user int n -scan.nextInt I/ Taking input of the number of integers System.out.print(What are you ranges?);//Printing Message to the user int min scan.nextInt); //Taking the lower bound int max scan.nextInt(); //Taking the upper bound 10 12 System.out.print(The generated odd numbers are ) 13 generateodd(n,min, max);//Calling the generateOdd funtion 14 15 System.out.print(The generated even numbers are) 16 17 18 public static void generate0dd(int n,int r1,int r2) 19 20 21 Random random- new Random(); //Creating the Object of Random class System.out.println()1/ NextLine. generateEven(n,min,max); I/Calling the generateEven) funtion int count-1; while(count<-n) //The Loop will execute n times 23 24 25 26 27 28 int randomNum-random .nextInt((r2+1)-r1)+r1; //Generating Random Numbers between r1 and r2 both İnclusive if(randonllum%2 ! 0) //checking if the random number genrated is odd or not. stem.out.print (randomNum+)s //f the number is odd we print it and increase the counter count++ Activate Windows Go to Settings to activate Windows Sy av 17:4 CRLF UTF-8 Java O files

The Code Used:-

import java.util.*;
class RandomNumber
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("How many integers you have? ");
int n = scan.nextInt();
System.out.print("What are you ranges? ");
int min = scan.nextInt();
int max = scan.nextInt();
System.out.print("The generated odd numbers are ");
generateOdd(n,min,max);
System.out.println();
System.out.print("The generated even numbers are ");
generateEven(n,min,max);
}
public static void generateOdd(int n,int r1,int r2)
{
int count=1;
Random random = new Random();
while(count<=n)
{
int randomNum = random.nextInt((r2+1)-r1)+r1;
if(randomNum%2 != 0)
{
System.out.print(randomNum+" ");
count++;
}
}
}
public static void generateEven(int n,int r1,int r2)
{
int count=1;
Random random = new Random();
while(count<=n)
{
int randomNum = random.nextInt((r2+1)-r1)+r1;
if(randomNum%2 == 0)
{
System.out.print(randomNum+" ");
count++;
}
}
}
}

I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.

Add a comment
Know the answer?
Add Answer to:
java 1. Write an application that generates n random integers in the range provided the user....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT