0% found this document useful (0 votes)
2 views1 page

Pgm3

The document contains a Java program that searches for a user-specified number in a predefined array of integers. It prompts the user to enter a number, checks if that number exists in the array, and prints whether the number is present or not. The program utilizes a simple for-each loop for the search operation and employs a boolean flag to track the result.

Uploaded by

wasimrajaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Pgm3

The document contains a Java program that searches for a user-specified number in a predefined array of integers. It prompts the user to enter a number, checks if that number exists in the array, and prints whether the number is present or not. The program utilizes a simple for-each loop for the search operation and employs a boolean flag to track the result.

Uploaded by

wasimrajaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;

public class SearchInArray {


public static void main(String[] args) {
// Initialize the array
int[] numbers = {10, 25, 30, 45, 60, 75};

// Get the number to search from the user


Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to search: ");
int target = scanner.nextInt();

boolean found = false;

// Search for the number in the array


for (int num : numbers) {
if (num == target) {
found = true;
break;
}
}

// Print the result


if (found) {
System.out.println(target + " is present in the array.");
} else {
System.out.println(target + " is not present in the array.");
}

scanner.close();
}
}

You might also like