Computer Applications

Write a program to accept 10 numbers in a Single Dimensional Array. Pass the array to a method Search(int m[], int ns) to search the given number ns in the list of array elements. If the number is present, then display the message 'Number is present' otherwise, display 'number is not present'.

Java

User Defined Methods

70 Likes

Answer

import java.util.Scanner;

public class KboatSDASearch
{
    public void search(int m[], int ns) {
        boolean found = false;
        for (int i = 0; i < m.length; i++) {
            if (m[i] == ns) {
                found = true;
                break;
            }
        }
        if (found)
            System.out.println("Number is present");
        else
            System.out.println("Number is not present");
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int arr[] = new int[10];
        System.out.println("Enter 10 numbers");
        
        for (int i = 0; i < arr.length; i++) {
            arr[i] = in.nextInt();
        }
        
        System.out.print("Enter number to search: ");
        int num = in.nextInt();
        
        KboatSDASearch obj = new KboatSDASearch();
        obj.search(arr, num);
    }
}

Variable Description Table

Program Explanation

Output

Answered By

15 Likes


Related Questions