One of the most interesting problem in programming is to ask the user to enter a series of number and then our program will determine which of the given number is the smallest and the highest number from the series of number by the user. Normally the data structure that we will use in this problem is array to be specific one dimensional array so to speak and then we will use for loop statement and if statement to compare each numbers and to check which of the given number is the smallest and highest.
I called this program Highest and Smallest Number in Java what the program does is to prompt or ask the user how many numbers to be accepted and to be process. After the user provide the values in our program it will compare and determine what is the smallest and the highest in a given list of values.
The program is very simple and easy to understand. I hope you will find my work useful in learning how to program in Java. If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
People here in the Philippines can reach me at my mobile number 09173084360.
Thank you very much and Happy Programming.
Sample Program Output
Program Listing
// smallest_biggest_number.java
// Written By: Mr. Jake R. Pomperada, MAED-IT
// Date : July 7, 2015
// Email Address: jakerpomperada@yahoo.com
// jakerpomperada@gmail.com
// Problem : Write a program in Java that will check what is the highest and
// lowest number given by the user using one dimensional array.
import java.util.Scanner;
public class smallest_biggest_number {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int[] num= new int[10];
System.out.print("\t\t SMALLEST AND HIGHEST NUMBER ");
System.out.print("\n\n");
System.out.print("How many items ? : ");
int n=input.nextInt();
int smallest = Integer.MAX_VALUE;
int biggest =Integer.MIN_VALUE;
for(int i =0;i<n;i++) {
int y=i+1;
System.out.print("Enter item value no. " + y + " value : ");
num[i]=input.nextInt();
if(num[i] < smallest) {
smallest = num[i];
}
if (num[i] > biggest) {
biggest = num[i];
}
}
System.out.println();
System.out.println("The Smallest number is " +smallest + ".");
System.out.println("The Biggest number is " +biggest +".");
System.out.println();
System.out.println("\t\t END OF PROGRAM");
System.out.println("\n\n");
}
}
No comments:
Post a Comment