Search Blog

Biyernes, Enero 18, 2013

Write a program that asks the user to enter 5 numbers and display the numbers in descending order.


Java Codes

--+o0O0o+--


Write a program that asks the user to enter 5 numbers and display the numbers in descending order.

Code:

import java.util.Scanner;

public class MachineProblem2 {

    public static void main(String[] args) {
   
    Scanner input_user = new Scanner(System.in);
   
    int[] num = new int[5];
    int temp;
       
        System.out.println("******************************************");
      
        System.out.println("   Enter Five(5) Numbers");

System.out.println("*****************************************");
        
        for(int x = 0; x < 5;x++)
        {
            System.out.print(" Number" + (x+1) + " :");
            num[x] = input_user.nextInt();
           
        }
       
        for(int w = 0;w < 4;w++)
        {
            for(int y = 0;y < 4;y++)
            {
                if(num[y] < num[y + 1])
                {
                 temp = num[y];
                 num[y] = num[y + 1];
                 num[y + 1] = temp;
                }
            }
        }
        System.out.println("******************************************");

        System.out.println(" Descending Order of the Numbers");

System.out.println("******************************************");
        
for(int z = 0;z < 5;z++)
        {
            System.out.println(num[z]);
        }
    }
}



To make this code Ascending just change this statement 
if(num[y] < num[y + 1]).Change the < (less than sign) to > (greater than sign).

Walang komento:

Mag-post ng isang Komento