Friday 19 August 2016

Chapter 6 Exercise 5, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*6.5 (Sort three numbers) Write a method with the following header to display three numbers in increasing order: public static void displaySortedNumbers( double num1, double num2, double num3) Write a test program that prompts the user to enter three numbers and invokes the method to display them in increasing order.

import java.util.Scanner;
 
public class ProgrammingExercise6_5 {
 
 public static void main(String[] args) {
 
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter three numbers:");
  double num1 = input.nextDouble();
  double num2 = input.nextDouble();
  double num3 = input.nextDouble();
 
  displaySortedNumbers(num1, num2, num3);
 
 }
 
 public static void displaySortedNumbers(double num1, double num2,
   double num3) {
 
  if (num3 > num2) {
   double temp = num2;
   num2 = num3;
   num3 = temp;
  }
 
  if (num2 > num1) {
   double temp = num1;
   num1 = num2;
   num2 = temp;
  }
 
  if (num3 > num2) {
   double temp = num2;
   num2 = num3;
   num3 = temp;
  }
 
  System.out.println("The numbers is increasing order is " + num3 + " "
    + num2 + " " + num1);
 
 }
 
}

No comments :

Post a Comment