Write a program that initializes an array of size 10 with 10 integer values betw
ID: 3769076 • Letter: W
Question
Write a program that initializes an array of size 10 with 10 integer values between 0 and 30 (choose any 10 distinct values and use initialization list). The program then asks the user to enter an integer number between 0 and 30. The program will check if the entered number is present in the array or not. Use a logical variable found that you initialize to false (0). If you find the entered number make it true (1). After finishing checking using a loop, print a message based on the value of the variable found.Explanation / Answer
import java.util.Arrays;
import java.util.Scanner;
public class NewClass3 {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int find;
boolean check;
check = false;
int ar[] = new int[10];
int p;
for(int j=0;j<10;j++)
{
p=(int)(Math.random()*30);
if(leniarSearch(ar,p)==-1)
{
ar[j]=p;
}
}
System.out.println("Enter the number to check between 0 and 30");
find=sc.nextInt();
for (int c = 0; c < 10; c++)
{
if (ar[c] == find) /* Searching element is present */
{
check=true;
break;
}
}
if(check)
{
System.out.println("Number is found");
}
else
{
System.out.println("Number not found");
}
}
static int leniarSearch(int[] ar,int key)
{
for (int c = 0; c < ar.length; c++)
{
if (ar[c] == key)
{
return c;
}
}
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.