Wednesday, January 24, 2018

Solution for the assignments find number in array

Note : Save this class inside FindInArray.java file under java project in Eclipse IDE

import java.util.Scanner;

public class FindInArray
{
public static void main(String[] args)
{
// create object of scanner to get data from keyboard
Scanner s = new Scanner(System.in);

System.out.print("Enter size for array:");
int size = s.nextInt();

// create an array of given size
int[] array = new int[size];

// loop to store data inside array using for loop
for (int i = 0; i < array.length; i++)
{
System.out.print("Input value for index " + i + " of array : ");
// get a value from keyboard and store it inside array
array[i] = s.nextInt();
}

// show a message
System.out.println("** array initialized **");

// show array using for loop
System.out.println("\nArray is given below ==>");
for (int i = 0; i < array.length; i++)
{
System.out.print("array[" + i + "] = " + array[i] + ", ");
}

// show a message to input a number
System.out.print("\n\nInput a number to search inside array :");

// input int value from keyboard and store it
int number = s.nextInt();

// show a menu
System.out.println("\n\n## Menu ##");
System.out.println("1 = to find first index of "+number+" inside array");
System.out.println("2 = to find last index of "+number+" inside array");
System.out.print("3 = to find all indexes of "+number+
                " inside array also its frequency =>");

// input int value from keyboard and store it
int choice = s.nextInt();

// this variable will keep track if a number is found in array or not
// i will store 1 inside this variable if number is found else it will store 0
int found = 0;

System.out.println("\n\n## Result ## \n");

if (choice == 1)
{
// fetch data from array using for loop
for (int i = 0; i < array.length; i++)
{
// check if number is equal to the i'th element of array
if (number == array[i])
{
// show the index at which number was found
System.out.println("First index of " + number + " inside array is " + i);

// store 1 in found since number is found in array
found = 1;

// terminate the loop using break keyword
break;
}
}

if(found == 0)
{
System.out.println(number+" not found in array");
}

}
else
if (choice == 2)
{
// fetch data from array using for loop from the last index
for (int i = array.length - 1; i >= 0; i--)
{
// check if number is equal to the i'th element of array
if (number == array[i])
{
// show the index at which number was found
System.out.println("Last index of " + number + " inside array is " + i);

// store 1 in found since number is found in array
found = 1;

// terminate the loop using break keyword
break;
}
}

if(found == 0)
{
System.out.println(number+" not found in array");
}

}
else if (choice == 3)
{
// this variable will keep frequency of number inside array
int counter = 0;

// fetch data from array using for loop
for (int i = 0; i < array.length; i++)
{
// check if number is equal to the i'th element of array
if (number == array[i])
{
// show the index at which number was found
System.out.println("Index of " + number + " inside array is " + i);

// store 1 in found since number is found in array
found = 1;

// increase the counter
counter++;
}
}

if(found == 1)
{
System.out.println("frequency of " + number + " in array is " + counter);
}
else
{
System.out.println(number+" not found in array");
}

}
else
{
System.out.println("##### invalid choice");
}

}
}

1 comment: