C Program to Determine the Largest Number Among Three Numbers

Finding the largest number among three numbers is a common task in programming. This article will cover various C Program to Determine the Largest Number Among Three Numbers. We will explore multiple examples, each demonstrating a different approach to solve the problem. Additionally, we will discuss the prerequisites, provide detailed explanations for each example, and conclude with a summary of what we’ve learned.

Prerequisites

Before we delve into the examples, ensure you have the following prerequisites:

  • A C compiler (such as GCC)
  • A text editor or IDE for writing your C code
  • Basic understanding of C programming concepts, especially conditionals and functions

1. Finding the Largest Number

In this section, we will look at several methods to determine the largest number among three numbers in C.

1.1 Using Simple if-else Statements

Example 1: Determine the Largest Number Using Simple if-else Statements

This method uses basic if-else statements to find the largest number among three numbers.

Code

C
#include <stdio.h>

int main() {
    int num1, num2, num3;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    if (num1 >= num2 && num1 >= num3) {
        printf("The largest number is %d\n", num1);
    } else if (num2 >= num1 && num2 >= num3) {
        printf("The largest number is %d\n", num2);
    } else {
        printf("The largest number is %d\n", num3);
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the numbers: scanf reads three integers from the user.
  • Check for the largest number: if-else statements determine the largest number.
  • Output the result: printf displays the largest number.

Output

C
Enter three numbers: 5 10 15
The largest number is 15

1.2 Using if and Logical Operators

Example 2: Determine the Largest Number Using if and Logical Operators

This method uses if statements combined with logical operators to find the largest number among three numbers.

Code

C
#include <stdio.h>

int main() {
    int num1, num2, num3;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    if (num1 > num2 && num1 > num3) {
        printf("The largest number is %d\n", num1);
    } else if (num2 > num1 && num2 > num3) {
        printf("The largest number is %d\n", num2);
    } else {
        printf("The largest number is %d\n", num3);
    }

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the numbers: scanf reads three integers from the user.
  • Check for the largest number using if and logical operators: if statements determine the largest number.
  • Output the result: printf displays the largest number.

Output

C
Enter three numbers: 25 15 10
The largest number is 25

1.3 Using a Function

Example 3: Determine the Largest Number Using a Function

This method encapsulates the logic to find the largest number in a separate function for better modularity.

Code

C
#include <stdio.h>

int findLargest(int num1, int num2, int num3);

int main() {
    int num1, num2, num3;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    int largest = findLargest(num1, num2, num3);
    printf("The largest number is %d\n", largest);

    return 0;
}

int findLargest(int num1, int num2, int num3) {
    if (num1 >= num2 && num1 >= num3) {
        return num1;
    } else if (num2 >= num1 && num2 >= num3) {
        return num2;
    } else {
        return num3;
    }
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Declare a function: int findLargest(int num1, int num2, int num3) to find the largest number.
  • Input the numbers: scanf reads three integers from the user.
  • Call the function: findLargest(num1, num2, num3) determines the largest number and returns it.
  • Output the result: printf displays the largest number.

Output

C
Enter three numbers: 8 16 4
The largest number is 16

1.4 Using Ternary Operator

Example 4: Determine the Largest Number Using Ternary Operator

This method uses the ternary operator to find the largest number among three numbers in a compact form.

Code

C
#include <stdio.h>

int main() {
    int num1, num2, num3, largest;

    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);

    printf("The largest number is %d\n", largest);

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the numbers: scanf reads three integers from the user.
  • Determine the largest number using ternary operator: A nested ternary operator finds the largest number.
  • Output the result: printf displays the largest number.

Output

C
Enter three numbers: 45 30 20
The largest number is 45

1.5 Using Arrays

Example 5: Determine the Largest Number Using Arrays

This method stores the numbers in an array and iterates through the array to find the largest number.

Code

C
#include <stdio.h>

int main() {
    int numbers[3];
    int i, largest;

    printf("Enter three numbers: ");
    for (i = 0; i < 3; ++i) {
        scanf("%d", &numbers[i]);
    }

    largest = numbers[0];
    for (i = 1; i < 3; ++i) {
        if (numbers[i] > largest) {
            largest = numbers[i];
        }
    }

    printf("The largest number is %d\n", largest);

    return 0;
}

Explanation

  • Include necessary header: #include <stdio.h> for input/output functions.
  • Input the numbers into an array: scanf reads three integers into the array numbers.
  • Find the largest number: Iterate through the array to determine the largest number.
  • Output the result: printf displays the largest number.

Output

C
Enter three numbers: 12 34 56
The largest number is 56

2. Conclusion

In this article, we explored various methods to determine the largest number among three numbers in C: using simple if-else statements, using if and logical operators, using a function, using the ternary operator, and using arrays. Each method demonstrates different aspects of handling conditions and iterations in C programming. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in decision-making and modular programming in C.

Using these examples as a guide, you can confidently determine the largest number among three in various ways depending on your requirements, making your programs more flexible and efficient.