Go to Top

Wednesday, 28 September 2011

To print maximum and minimum numbers

Question 45 : Write a program in Java to print the maximum and minimum of the numbers entered in the form of a list by the user. Program should display the results when the user enters 0.

Java Program : 

import java.io.*;
class max_min
{
    public static void main()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter any number or '0' to exit\n\n");
        int n=0,i=9;
        System.out.print("Enter a number : ");
        n=Integer.parseInt(br.readLine());
        System.out.print("\n");
        int max=n,min=n;
        do
        {
            if(n>max)
            max=n;  
            if(n<min)
            min=n;
            System.out.print("Enter a number : ");
            n=Integer.parseInt(br.readLine());
            System.out.print("\n");
            if(n==0)
            i=n;
        }while(i!=0);
        System.out.print("The maximum number is : "+max+"\n");
        System.out.println("\nThe minimum number is : "+min);
    }
}

2 comments:

ShareThis