Amazon Prime




 
Tagged
  • Flow Chart
  •  

    Sort Three Numbers

    Problem Statement

    1Problem Description : Given three integers Write Algorithm, Flowchart and Program to display them in ascending order.
    2
    3Example 1 :
    4 Input : 2, 3, 1
    5 Output : 1, 2, 3
    6 Explanation : 1, 2, 3 are in ascending order for given numbers
    7
    8Example 2 :
    9 Input : 5, 2, 7
    10 Output : 2, 5, 7
    11 Explanation : 2, 5, 7 are in ascending order for given numbers

    Algorithm

    1Input : Three numbers n1, n2, n3
    2Step 1: Start
    3Step 2: Read number n1, n2, n3
    4Step 3: if n1 <= n2 and n1 <= n3 go to Step 4 else go to step 7
    5Step 4: if n2 <= n3 go to step 5 else go to step 6
    6Step 5: print n1, n2, n3 and go to step 14
    7Step 6: print n1, n3, n2 and go to step 14
    8step 7: if n2 <= n1 and n2 <= n3 go to step 8 else go to step 11
    9step 8: if n1 <= n3 go to step 9 else go to step 10
    10step 9: print n2, n1, n3 and go to step 14
    11step 10: print n2, n3, n1 and go to step 14
    12step 11: if n1 <= n2 go to step 12 else go to step 13
    13step 12: print n3, n1, n2 and go to step 14
    14step 13: print n3, n2, n1 and go to step 14
    15step 14: Stop
    16Output: n1, n2, n3 in sorted order

    Code Implementation

    1. Java
    2. Golang
    3. C

    1import java.util.Scanner;
    2
    3public class SortThreeNumbers {
    4
    5 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 }
    15
    16 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 45
    3Output : 12 45 54
     
    1Output 2 :
    2Input : 12 21 10
    3Output : 10 12 21
     

  • Flow Chart
  •  
    ...