Skip to content
Snippets Groups Projects
Commit db3a7bcc authored by Syed Naqvi's avatar Syed Naqvi
Browse files

Solutoin file 1

parent decede86
No related branches found
No related tags found
No related merge requests found
.DS_Store 0 → 100644
File added
// Import the Scanner class to take user input
import java.util.Scanner;
public class ArrayFindingMax {
public static void main(String[] args) {
// Create a Scanner object to take input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the number of elements in the array
System.out.print("Enter the number of elements: ");
int N = scanner.nextInt(); // Read user input and store it in variable 'N'
// Declare an array of size 'N' to store numbers entered by the user
int[] numbers = new int[N];
// Ask the user to enter 'N' numbers
System.out.println("Enter " + N + " numbers:");
for (int i = 0; i < N; i++) { // Loop runs from 0 to N-1
numbers[i] = scanner.nextInt(); // Store user input in the array
}
// Assume the first element is the largest (initial max value)
int max = numbers[0];
// Loop through the array starting from the second element (index 1)
for (int i = 1; i < N; i++) {
if (numbers[i] > max) { // If the current element is greater than 'max'
max = numbers[i]; // Update 'max' with the current element
}
}
// Print the largest number found in the array
System.out.println("The largest number is: " + max);
// Close the scanner to prevent resource leaks
scanner.close();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment