From c5e5787b58dd9030e894701e4fb215fb3119eef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=A6=20=F0=9D=92=82=F0=9D=92=95=F0=9D=92=89?= =?UTF-8?q?=F0=9D=92=82=F0=9D=92=93=F0=9D=92=97=2E=F0=9D=92=85=F0=9D=92=86?= =?UTF-8?q?=F0=9D=92=97?= <144611888+codedbyatharv@users.noreply.github.com> Date: Sat, 18 Oct 2025 10:05:40 +0530 Subject: [PATCH] Add FindInArray program for number search This Java program allows users to input an array size and elements, searches for a specified number, and prints the positions where the number occurs. --- Findarray.java | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Findarray.java diff --git a/Findarray.java b/Findarray.java new file mode 100644 index 0000000..f62ad36 --- /dev/null +++ b/Findarray.java @@ -0,0 +1,56 @@ +/* + * FindInArray.java + * ---------------- + * Simple Java program to find a given number in an array. + * + * Features: + * - Takes user input for array size and elements + * - Searches for a target number (linear search) + * - Prints all positions where the number occurs + * - Clean and easy to understand — great Hacktoberfest contribution + * + * Author: + * Hacktoberfest 2025 Contribution + * License: MIT + */ + +import java.util.*; + +public class FindInArray { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.println("🔍 Find Number in Array - Hacktoberfest 2025 Edition 🔍"); + System.out.print("Enter number of elements: "); + int n = sc.nextInt(); + + if (n <= 0) { + System.out.println("Array size must be positive!"); + return; + } + + int[] arr = new int[n]; + System.out.println("Enter " + n + " integers:"); + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + + System.out.print("Enter the number to find: "); + int target = sc.nextInt(); + + List positions = new ArrayList<>(); + for (int i = 0; i < n; i++) { + if (arr[i] == target) { + positions.add(i); // store index + } + } + + if (positions.isEmpty()) { + System.out.println("❌ Number " + target + " not found in the array."); + } else { + System.out.println("✅ Number " + target + " found at position(s): " + positions); + } + + sc.close(); + } +}