Question: Take an array as input from the user. Search for a given number x and print the index at which it occurs.
Ans:
package HelloWorld;
import java.util.Scanner; //this is a Scanner class
public class LearningJava {
public static void main (String [] args) {
Scanner scan = new Scanner (System.in);
int size = scan.nextInt();
int [] numBer = new int [size];
for(int i = 0;i < size ; i++) {
numBer[i] = scan.nextInt();
}
//
int x = scan.nextInt();
for(int j = 0 ; j< numBer.length; j ++) {
if(numBer[j] == x) {
System.out.println("Number Found:" + j );
}
}
}
}
Comments
Post a Comment