Largest of Two Numbers: flowchart, algorithm and program

This program reads two integers, a and b, and prints the larger one. It compares them with a single if-else: if a is greater than b, a is printed, otherwise b is printed.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read a.
  3. Step 3: Read b.
  4. Step 4: If a > b, go to step 5; otherwise go to step 6.
  5. Step 5: Print a as Largest. Go to step 7.
  6. Step 6: Print b as Largest.
  7. Step 7: Stop.

Pseudocode

BEGIN
    READ a
    READ b
    IF a > b THEN
        PRINT a as Largest
    ELSE
        PRINT b as Largest
    END IF
END

Program in C, Python and Java

Largest of Two Numbers program in C

#include <stdio.h>

int main() {
    int a, b;
    printf("Enter a: ");
    scanf("%d", &a);
    printf("Enter b: ");
    scanf("%d", &b);
    if (a > b)
        printf("Largest = %d\n", a);
    else
        printf("Largest = %d\n", b);
    return 0;
}

Largest of Two Numbers program in Python

a = int(input("Enter a: "))
b = int(input("Enter b: "))
if a > b:
    print("Largest =", a)
else:
    print("Largest =", b)

Largest of Two Numbers program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a: ");
        int a = sc.nextInt();
        System.out.print("Enter b: ");
        int b = sc.nextInt();
        if (a > b)
            System.out.println("Largest = " + a);
        else
            System.out.println("Largest = " + b);
    }
}

Sample input: a = 4, b = 9
Output: Enter a: Enter b: Largest = 9

How it works

  • scanf reads the two integers into a and b, after printing a prompt for each.
  • The condition a > b is checked once. It is either true or false.
  • If a > b is true, the program prints a as the largest.
  • If it is false, the else branch prints b. This also covers the case where a and b are equal, since then b is just as large.
  • With a = 4 and b = 9, the check 4 > 9 is false, so the else branch runs and prints Largest = 9.

Dry run (trace table)

StepLineBoxabOutput
13Start
26Read a4Enter a:
38Read b49Enter b:
49a > b?49
512Print b as Largest49Largest = 9
613Stop49

Time and space complexity

Time: O(1) · Space: O(1). Only one comparison is made and a fixed number of variables are used.

Common mistakes

  • Forgetting the & in scanf("%d", &a), so the value is not stored in the variable.
  • Using = instead of == or > in the condition, for example writing if (a = b), which assigns instead of compares.
  • Assuming equal numbers need a special case. When a == b, the else branch prints b, which is correct because both are the same.

Viva questions

Q1. What does this program do, and what is the role of the condition a > b on line 9?

It reads two integers and prints the larger one. The condition a > b is true when a is bigger, so a is printed; otherwise the else branch prints b.

Q2. Why do we write &a and &b in scanf but just a and b in printf?

scanf needs the address of the variable so it can store the input value there. printf only needs the value to display, so no & is used.

Q3. What does the program print if a and b are equal, say 5 and 5? Is that correct?

The condition a > b is false, so the else branch runs and prints b, which is 5. The answer is still correct because both numbers are equal.

Q4. Does the program work for negative numbers, for example a = -3 and b = -7?

Yes. The > operator compares signed integers correctly, so -3 > -7 is true and it prints -3 as the largest.

Q5. What does %d mean and what happens if the user types a letter or a decimal like 3.5?

%d reads and prints an int. If a letter is typed, scanf fails and the variable keeps a garbage value. For 3.5 it reads 3 and leaves .5 in the input buffer. The program does not check the return value of scanf.

Q6. What is the time complexity of this program?

O(1), constant time. It does exactly one comparison no matter what the values are.

Q7. What would you change to find the largest of three numbers?

Read a third number c and use nested if-else or a combined condition such as if (a >= b && a >= c) for a, else if (b >= c) for b, else c. Alternatively, keep a variable max and compare it with each number.

Q8. Can you write the same logic without if-else? What would you use?

Yes, with the ternary operator: printf("Largest = %d\n", (a > b) ? a : b);. It does the same comparison in a single expression.

Frequently asked questions

What happens if both numbers are equal?

The condition a > b is false, so the else branch prints b. Since a and b are equal, the output is still correct.

Can I find the largest of two numbers without if-else?

Yes. You can use the ternary operator, like (a > b) ? a : b, or a max function in languages that have one. It does the same single comparison.

How many comparisons does this program make?

Just one, a > b. That is enough to decide which of the two numbers is larger.

More programs

← All programs

Try AI Flowchart free →