Greatest Among Three Numbers — C | C++ | Java | Python

Greatest Among Three Numbers — How to write the program

Finding the largest of three numbers is a classic beginner coding problem. In this post you’ll get working, ready-to-run examples in C, C++, Java, and Python, plus explanations, complexity, and best-practice tips so your solution is clean and efficient.


Why this matters

This problem teaches conditional logic, comparisons, and basic input/output for multiple languages. It’s frequently asked in interviews and useful for practicing language syntax differences.


Approach & complexity

Common approaches:

  1. Use pairwise comparisons (if-else) — simplest and clear.
  2. Use built-in max() functions or the conditional (ternary) operator where available.

Time complexity: O(1) — constant time, only a few comparisons.
Space complexity: O(1) — constant extra memory.


Tips for SEO (short):

  • Put the primary keyword in the first paragraph and H1.
  • Use code blocks with clear language tags (```c, cpp `, java , ```python ).
  • Add an image with an ALT that contains the keyword.
  • Use internal links to related tutorials and an authoritative external link.

Example 1 — C

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter three integers: ");
    if (scanf("%d %d %d", &a, &b, &c) != 3) {
        printf("Invalid input\n");
        return 1;
    }

    int greatest = a;
    if (b > greatest) greatest = b;
    if (c > greatest) greatest = c;

    printf("Greatest number is: %d\n", greatest);
    return 0;
}

Notes: Use scanf carefully and validate input. This uses pairwise comparisons and is portable C.

Example 2 — C++

#include <iostream>
#include <algorithm> // for std::max

int main() {
    int a, b, c;
    std::cout << "Enter three integers: ";
    if (!(std::cin >> a >> b >> c)) {
        std::cerr << "Invalid input\n";
        return 1;
    }

    // Option A: using std::max twice
    int greatest = std::max(a, std::max(b, c));

    // Option B: manual comparisons
    // int greatest = a;
    // if (b > greatest) greatest = b;
    // if (c > greatest) greatest = c;

    std::cout << "Greatest number is: " << greatest << '\n';
    return 0;
}

Notes: std::max is concise. Input validation uses std::cin state.


Example 3 — Java

import java.util.Scanner;

public class GreatestOfThree {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter three integers: ");
        if (!sc.hasNextInt()) {
            System.out.println("Invalid input");
            sc.close();
            return;
        }
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        sc.close();

        // Using Math.max
        int greatest = Math.max(a, Math.max(b, c));

        System.out.println("Greatest number is: " + greatest);
    }
}

Notes: Prefer Scanner for console input in simple programs. For production or heavy input, use buffered IO.


Example 4 — Python

def main():
    try:
        a, b, c = map(int, input("Enter three integers: ").split())
    except ValueError:
        print("Invalid input")
        return

    # Option A: built-in max
    greatest = max(a, b, c)

    # Option B: manual comparison
    # greatest = a
    # if b > greatest:
    #     greatest = b
    # if c > greatest:
    #     greatest = c

    print("Greatest number is:", greatest)

if __name__ == "__main__":
    main()

Notes: Python’s max() is direct and readable. Validate or handle exceptions for robust code.

Common pitfalls & edge cases

  • All numbers equal — program should still return that number.
  • Negative numbers — comparisons work fine if types are correct.
  • Non-integer input — handle input errors gracefully.
  • Large numbers — ensure the chosen data type can hold the input (e.g., long long in C/C++ if needed).
Scroll to Top