![Amazon Prime](https://technologystrive.com/images/amazonPrime.jpg)
Sort Three Numbers
Problem Statement
1Problem Description : Given three integers Write Algorithm, Flowchart and Program to display them in ascending order.23Example 1 :4 Input : 2, 3, 15 Output : 1, 2, 36 Explanation : 1, 2, 3 are in ascending order for given numbers78Example 2 :9 Input : 5, 2, 710 Output : 2, 5, 711 Explanation : 2, 5, 7 are in ascending order for given numbers
Algorithm
1Input : Three numbers n1, n2, n32Step 1: Start3Step 2: Read number n1, n2, n34Step 3: if n1 <= n2 and n1 <= n3 go to Step 4 else go to step 75Step 4: if n2 <= n3 go to step 5 else go to step 66Step 5: print n1, n2, n3 and go to step 147Step 6: print n1, n3, n2 and go to step 148step 7: if n2 <= n1 and n2 <= n3 go to step 8 else go to step 119step 8: if n1 <= n3 go to step 9 else go to step 1010step 9: print n2, n1, n3 and go to step 1411step 10: print n2, n3, n1 and go to step 1412step 11: if n1 <= n2 go to step 12 else go to step 1313step 12: print n3, n1, n2 and go to step 1414step 13: print n3, n2, n1 and go to step 1415step 14: Stop16Output: n1, n2, n3 in sorted order
Code Implementation
- Java
- Golang
- C
1import java.util.Scanner;23public class SortThreeNumbers {45 public static void main(String[] args) {6 Scanner sc = new Scanner(System.in);7 System.out.print("Enter n1: \n");8 int n1 = sc.nextInt();9 System.out.print("Enter n2: \n");10 int n2 = sc.nextInt();11 System.out.print("Enter n3: \n");12 int n3 = sc.nextInt();13 new SortThreeNumbers().sort(n1, n2, n3);14 }1516 public void sort(int n1, int n2, int n3) {17 if(n1 <= n2 && n1 <= n3) {18 if(n2 <= n3) {19 System.out.printf("%d %d %d \n", n1, n2, n3);20 }else {21 System.out.printf("%d %d %d \n", n1, n3, n2);22 }23 }else if(n2 <= n1 && n2 <= n3) {24 if(n1 <= n3) {25 System.out.printf("%d %d %d \n", n2, n1, n3);26 }else {27 System.out.printf("%d %d %d \n", n2, n3, n1);28 }29 }else {30 if(n1 <= n2){31 System.out.printf("%d %d %d \n", n3, n1, n2);32 }else {33 System.out.printf("%d %d %d \n", n3, n2, n1);34 }35 }36 }37}
1Output 1 :2Input : 54 12 453Output : 12 45 54
1Output 2 :2Input : 12 21 103Output : 10 12 21