write a program to take 30 numbers and display the highest and lowesf number on basis of loop
Answers
Answer:
import java.util.*;
public class Question {
public static void main(String[] args) {
int smallest = 0;
int large = 0;
int num;
System.out.println("enter the number");
//how many number you want to enter
Scanner input = new Scanner(System.in);
int n = input.nextInt();
num = input.nextInt();
smallest = num;
//assume first entered number as small one
// i starts from 2 because we already took one num value
for (int i = 2; i < n; i++) {
num = input.nextInt();
//comparing each time entered number with large one
if (num > large) {
large = num;
}
//comparing each time entered number with smallest one
if (num < smallest) {
smallest = num;
}
}
System.out.println("the largest is:" + large);
System.out.println("Smallest no is : " + smallest);
}
}
Question:-
write a program to take 30 numbers and display the highest and lowest number on basis of loop.
Program:-
import java.util.*;
class Number
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 30 numbers...");
int max=Integer.MIN_VALUE, min=Integer.MAX_VALUE,x;
for(int i=1;i<=30;i++)
{
x=sc.nextInt();
if(x>max)
max=x;
if(x<min)
min=x;
}
System.out.println("Largest number: "+max);
System.out.println("Smallest number: "+min);
}
}