Write a program in java to input an integer array and perform the following operations i) Display the largest number from the array ii) Display the smallest number from the array iii) Display the sum of all the elements of the array
THE FOLLOWING PROGRAM SEGMENT SHOWS HOW TO PRINT THE ABOVE OPERATIONS:
Perform the following operations
i) Display the largest number from the array
ii) Display the smallest number from the array
iii) Display the sum of all the elements of the array
import java.util.*;
class Print
{
public static void main (String args[])
{
Scanner ob = new Scanner (System.in);
System.out.println("Enter the length of array");
int n = ob.nextInt();
int ar[] = new int [n];
System.out.println("Enter the elements of array");
for(int i = 0; i<n; i++)
{
ar[i] = ob.nextInt();
}
int l = ar[0]; int s = ar[0]; int sum = 0;
for(int i = 0; i<n; i++)
{
if(ar[i] < s)
{
s = ar[i];
if(ar[i]>l)
{
l = ar[i];
}
for(i = 0; i < ar[i] ; i++)
{
sum+= ar[i];
}
}
System.out.println("Largest = " +l);
System.out.println("Smallest =" +s);
System.out.println("Sum of all elements = "+sum);
}
}
}
Hope you guys found it helpful.
Please share it with your friends and comment down in the comment section below.
Follow https://askrisfor.blogspot.com/ for more such updates .
Visit us again.
Thank you!!
Comments
Post a Comment