Complete the Java code. The code should print “x is in numbers” if the integer x is one of the values stored in numbers. If x is not in numbers, your code should print “x is not in numbers”
public static void main(String[] args){
int[] numbers = <some array values>;
int x = <some value>;
}
The code is done in Java and it is simply understandable and for more clarity it is comments have been added.
CODE:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is
public. */
class Ideone
{
public static void main (String[] args) throws
java.lang.Exception
{
// array consisting numbers
int[] numbers = { 12, 11, 13, 5, 6
};
//finding the length of array
int n = numbers.length;
//number to be searched
int x = 13;
//variable to keep track of number
present in array or not
int flag=0;
//traversing the array from index 0
to n-1
for(int i=0;i<n;i++){
//checking the
condition
if(numbers[i]==x){
System.out.print(x + " is in numbers ");
flag=1;
break;
}
}
//checking the condition of element
not present in array
if(flag==0){
System.out.print(x + " is not in numbers ");
}
}
}
OUTPUT:
Success #stdin #stdout 0.08s 33884KB
13 is in numbers
Get Answers For Free
Most questions answered within 1 hours.