Computer Applications
Write a program to accept a set of 50 integers. Find and print the greatest and the smallest numbers by using scanner class method.
Answer
import java.util.Scanner;
public class KboatSmallLargeNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 50 integers:");
int small = Integer.MAX_VALUE, big = Integer.MIN_VALUE;
for (int i = 1; i <= 50; i++) {
int n = in.nextInt();
if (n > big)
big = n;
if (n < small)
small = n;
}
System.out.println("Smallest Number = " + small);
System.out.println("Largest Number = " + big);
}
}Variable Description Table
Program Explanation
Output
Related Questions
Differentiate between hasNextInt( ) and hasNextBoolean( )
Using scanner class, write a program to input temperatures recorded in different cities in °F (Fahrenheit). Convert and print each temperature in °C (Celsius). The program terminates when user enters a non-numeric character.
Write a program (using scanner class) to generate a pattern of a token in the form of a triangle or in the form of an inverted triangle depending upon the user's choice.
Sample Input/Output:
Enter your choice 1
*
* *
* * *
* * * *
* * * * *Enter your choice 2
* * * * *
* * * *
* * *
* *
*In a competitive examination, a set of 'N' number of questions results in 'True' or 'False'. Write a program by using scanner class to accept the answers. Print the frequency of 'True' and 'False'.