Sort Two Numbers
Problem Statement
1Problem Description : Given two integers Write Algorithm, Flowchart and Program to display them in ascending order.23Example 1 :4 Input : 3, 15 Output : 1, 36 Explanation : 1, 3 are in ascending order78Example 2 :9 Input : 5, 210 Output : 2, 511 Explanation : 2, 5 are in ascending order
Algorithm
1Input : Two numbers n1, n22Step 1: Start3Step 2: Read number n1, n24Step 3: if n1 <= n25Step 4: print n1, n2 and go to step 76Step 5: else7Step 6: print n2, n1 and go to step 78step 7: Stop9Output: n1, n2 in sorted order
Code Implementation
- Java
- Golang
- C
1import java.util.Scanner;23public class SortTwoNumbers {45 public static void main(String[] args) {67 int n1, n2;8 Scanner sc = new Scanner(System.in);910 System.out.print("Enter n1: ");11 n1 = sc.nextInt();12 System.out.print("Enter n2: ");13 n2 = sc.nextInt();1415 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 122Output : 12 54
Output 2 :
1Input : 12 212Output : 12 21