Amazon Prime




 
Tagged
  • Flow Chart
  •  

    Sort Two Numbers

    Problem Statement

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

    Algorithm

    1Input : Two numbers n1, n2
    2Step 1: Start
    3Step 2: Read number n1, n2
    4Step 3: if n1 <= n2
    5Step 4: print n1, n2 and go to step 7
    6Step 5: else
    7Step 6: print n2, n1 and go to step 7
    8step 7: Stop
    9Output: n1, n2 in sorted order

    Code Implementation

    1. Java
    2. Golang
    3. C

    1import java.util.Scanner;
    2
    3public class SortTwoNumbers {
    4
    5 public static void main(String[] args) {
    6
    7 int n1, n2;
    8 Scanner sc = new Scanner(System.in);
    9
    10 System.out.print("Enter n1: ");
    11 n1 = sc.nextInt();
    12 System.out.print("Enter n2: ");
    13 n2 = sc.nextInt();
    14
    15 if (n1 <= n2) {
    16 System.out.printf("%d, %d \n", n1, n2);
    17 } else {
    18 System.out.printf("%d, %d \n", n2, n1);
    19 }
    20 }
    21}

    Output 1 :

    1Input : 54 12
    2Output : 12 54
     

    Output 2 :

    1Input : 12 21
    2Output : 12 21
     

  • Flow Chart
  •  
    ...