
Largest of Three Numbers
Problem Statement
1Problem Description : Find the largest of three numbers.23Example 1 :4Input : a = 3, b = 7 and c = 45Output : 76Explanation : 7 is the largest number.78Example 2 :9Input : a = 12, b = 1 and c = 710Output : 1211Explanation : 12 is the largest number.
Algorithm
1Input : Three numbers a, b and c2Step 1: Start3Step 2: Read number a4Step 3: Read number b5Step 4: Read number c6Step 5: if a > b Goto Step 6, else Goto Step 77Step 6: if a > c Print a, else Print c8Step 7: if b > c Print b, else Print c9Step 8: Stop10Output : Largest number among a, b and c
Flow Chart
Code Implementation
- Java
- C
1import java.util.Scanner;23public class LargestOfThreeNumbers {45 public static void main(String[] args) {6 int a, b, c;7 Scanner sc = new Scanner(System.in);89 System.out.print("Enter a: ");10 a = sc.nextInt();11 System.out.print("Enter b: ");12 b = sc.nextInt();13 System.out.print("Enter c: ");14 c = sc.nextInt();1516 if (a > b) {17 if (a > c) {18 System.out.printf("Largest Number is : %d \n", a);19 } else {20 System.out.printf("Largest Number is : %d \n", c);21 }22 } else {23 if (b > c) {24 System.out.printf("Largest Number is : %d \n", b);25 } else {26 System.out.printf("Largest Number is : %d \n", c);27 }28 }29 }30}
Output 1:
1Enter a : 272Enter b : 33Enter c : 74Largest Number is : 27
Output 2:
1Enter a: 52Enter b: 173Enter c: 84Largest Number is : 17