Finding Minimum, Maximum, and Sum of Array Elements in Java
Java program to initialize given data in an array and find the minimum and maximum values along with the sum of given elements.
import java.util.*;
public class Array_Max_Min {
public static void main() {
Scanner Sc = new Scanner(System.in);
System.out.println(“Enter 5 values”);
int l = 0, s = 9, sum = 0;
int a[] = new int[5];
// Loop to input 5 values and calculate sum
for (int i = 0; i
Certainly! Here’s a rephrased version suitable for a blog post:
In this Java tutorial, we’ll create a program that allows us to find the minimum, maximum values, and sum of integers entered by the user. This exercise is useful for understanding basic array operations and conditional statements in Java programming.
We begin by importing the Scanner
class from java.util
, which enables us to read user input efficiently:
import java.util.Scanner;
Next, we define a public class named Array_Max_Min
with a main method where our program execution begins:
public class Array_Max_Min {
public static void main(String[] args) {
// Code goes here
}
}
Inside the main
method, we initialize variables to store the maximum (l
), minimum (s
) values, and the sum of all entered integers (sum
):
Scanner scanner = new Scanner(System.in);
System.out.println("Enter 5 values:");
int l = Integer.MIN_VALUE; // Initialize l to the smallest possible integer
int s = Integer.MAX_VALUE; // Initialize s to the largest possible integer
int sum = 0;
int[] array = new int[5]; // Create an array to store 5 integers
We use a loop to read 5 integers from the user, calculate their sum, and determine the minimum and maximum values:
for (int i = 0; i < 5; i++) {
array[i] = scanner.nextInt();
sum += array[i];
// Find maximum value
if (array[i] > l) {
l = array[i];
}
// Find minimum value
if (array[i] < s) {
s = array[i];
}
}
Finally, we display the results to the user:
System.out.println("Minimum value: " + s);
System.out.println("Maximum value: " + l);
System.out.println("Sum of elements: " + sum);
This Java program efficiently computes the minimum, maximum values, and sum of 5 integers entered by the user. It demonstrates fundamental concepts such as array handling, conditional statements (if
), and basic arithmetic operations. Feel free to modify and experiment with the code to deepen your understanding of Java programming.
This blog post outlines a practical Java programming exercise that enhances your skills in handling arrays and basic data operations. It provides a clear, step-by-step guide for beginners to understand and implement similar functionalities in their own Java projects.
In this post i have shared a java program which is mostly asked by students of computer science.
THANK YOU FOR VISITING OUR BLOG. For more related posts and anime, programing, gaming post please regularly check our blog.https://masterditech.com/how-to-start-programming-as-a-beginner/https://masterditech.com/top-5-programming-language-to-learn-in-2023/
THANK YOU!!!!!!!!!!!
Making money as a coder in 2025 is not much of a big or tough…
Lloyds Technology Centre, A tech and data company located in Hyderabad, India. They are part of…
How to Increase Height at Any Age Rise Above: How to Increase Height at Any…
In the fast-paced digital world, we often forget the timeless beauty that nature offers. Today,…
Top 10 Deadliest Kicks in Martial Arts: Master the Art of Knockouts Martial arts is…
How to Optimize Battery Life of Your Smartphone: Tips and Tricks for Prolonging Battery Health…
View Comments
May I simply say what a comfort to discover somebody who genuinely knows what they are talking about over the internet. You actually understand how to bring a problem to light and make it important. More people ought to check this out and understand this side of the story. I cant believe you arent more popular because you surely possess the gift.