Finding index of an Array Element
CODE
import java.util.Scanner;
import java.util.Random;
public class red {
public static void main (String args[]) {
Scanner bucky = new Scanner(System.in);
int array[] = {25,14,96,35,12};
int number, h=0;
System.out.println("Your array is");
for(int i : array) {
System.out.printf(i + " ");
}
System.out.println();
System.out.println("Now choose a number whose index you want to know");
number = bucky.nextInt();
for(int j=0;j<array.length;j++) {
if(number == array[j]) {
h=1;
System.out.println("The number is present in the defined array and its index is " + j);
break;
}
}
if(h==0) {
System.out.println("We cannot find the number inside");
}
}
}
OUTPUT
A. WHEN NUMBER IS PRESENT
B. WHEN NUMBER IS NOT PRESENT
Comments
Post a Comment