Write a java program that will read 10 integer values between 1 and 100 into an
ID: 3776115 • Letter: W
Question
Write a java program that will read 10 integer values between 1 and 100 into an array A. Then generate 10 random integer values in the same range and store them in an array B. You need to check whether array B has at least one value that is greater than any value in array A. If such a value exists print a message that shows this value, otherwise print a message that tells no such value exist
For example: Suppose you have input:
23 27 45 30 20 65 50 44 34 45
And suppose that the generated random integers are:
12 11 40 80 32 67 1 0 2 5
Then your output should be:
A value greater than all input values is found and it is: 80
However, assume that the generated random integers are:
19 20 17 1 3 6 5 11 19 18
Then the output should be:
A value does not exist
Explanation / Answer
package com;
import java.util.*;
public class Demo
{
public static void main( String args[] )
{
System.out.println("enter 10 integers beyween 1 and 100 ");
Scanner s=new Scanner(System.in);
int[] a=new int[10];
int[] b=new int[10];
for(int i=0;i<10;i++)
{
System.out.println("Integer "+i+1);
a[i]=s.nextInt();
}
// create random object
Random randomno= new Random();
for(int i=0;i<10;i++)
{
b[i]=randomno.nextInt(100);
}
boolean flag=false;
for(int i=0;i<10;i++)
{
int count=0;
for(int j=0;j<10;j++)
{
if(b[i]>a[j])count++;
}
if(count==10){
System.out.println(" A value greater than all input values exists and it is : "+b[i]);
flag=true;
break;
}
}
if(flag==false)
System.out.println(" a value does not exist");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.