How to Print Largest and Smallest Number in Java
Java program to find largest and smallest number in an array
Java program to find largest and smallest number in an array
In this tutorial, we will discuss the concept of a Java program to find the largest and smallest number in an array
In this topic, we learn how to find the smallest and largest element of an array(Collection of elements)
This program gets "n" number of elements and Enter the elements of the array as input from the user. then, this program finds and displays the smallest and largest elements from the array using for loops.
In this programs, we can see step by step procedure for completion of the program.
- array declaration
- Essential variable declaration
- Get input from the user for array length
- Get input from the user for array elements
- loop for find the smallest and largest value
- Display result on the screen
Find the Largest and Smallest value – integer Array
Program 1
import java.util.Scanner; class Large_Small_num1{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); System.out.print("Enter the number of elements in an array: "); int min,max; int n=scan.nextInt();//get input from user for array length int arr[]=new int[n]; //declaring an array of n elements //for loop takes input from user for(int i=0; i<n; i++){ System.out.print("Enter the element "+(i+1)+": "); arr[i]=scan.nextInt();//takes input from user for array } min=arr[0];//assume first element as smallest value max=arr[0];//assume first element as largest value for(int i=0; i<n; i++){ if(min>arr[i]){//loop for find minimum elements min=arr[i]; } if(max<arr[i]){ max=arr[i]; //loop for find maximum elements } } System.out.print("\nThe smallest value is: "+min); System.out.print("\nThe largest value is: "+max); }//display result on the result }
When the above code is compiled and executed, it produces the following results
Enter the number of elements in an array: 5 Enter the elements 1: 45 Enter the elements 2: 96 Enter the elements 3: 12 Enter the elements 4: 34 Enter the elements 5: 65 The smallest value is:12 The largest value is:96
Find the Largest and Smallest value – float Array
Program 2
import java.util.Scanner; class Large_Small_num{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); System.out.print("Enter the number of elements in an array: "); float min,max; int n=scan.nextInt();//get input from user for array length float arr[]=new float[n]; //declaring an array of n elements //for loop takes input from user for(int i=0; i<n; i++){ System.out.print("Enter the element "+(i+1)+": "); arr[i]=scan.nextFloat();//takes input from user for array } min=arr[0];//assume first element as smallest value max=arr[0];//assume first element as largest value for(int i=0; i<n; i++){ if(min>arr[i]){//loop for find minimun elements min=arr[i]; } if(max<arr[i]){ max=arr[i]; //loop for find maximun elements } } System.out.print("\nThe smallest value is: "+min); System.out.print("\nThe largest value is: "+max); }//display result on the result }
When the above code is compiled and executed, it produces the following results
Enter the number of elements in an array:5 Enter the element 1: 8.9 Enter the element 2: 5.2 Enter the element 3: 2.1 Enter the element 4: 5.6 Enter the element 5: 4.3 The smallest value is: 2.1 The largest value is:8.9
Suggested for you
Operator in Java language
For loop in Java language
If statements in Java language
Data type in Java language
Single dimension array in Java language
Related posts:
How to Print Largest and Smallest Number in Java
Source: https://code4coding.com/java-program-to-find-largest-and-smallest-number-in-an-array/
0 Response to "How to Print Largest and Smallest Number in Java"
Post a Comment