C Program to Find the Largest Among Three Numbers

Find the largest number among three numbers

C program to find the largest among three numbers can be computed by comparing them with each other. A number x is said to be the largest among three numbers when its value is greater than other two numbers say y and z. For example, 20 is the largest among three numbers 10, 15, 20.






1. Algorithm to find the largest among three numbers


// $x$, $y$, $z$ are three input numbers//


1. Check if $x$ is greater or equal to $y$

2. If step [1] is true, then check if $x$ is greater or equal to $z$

3. If step [2] is true, then print $x$ is the largest

4. If step [2] is false, then print $z$ is the largest

5. If step [1] is false, then check if $y$ is greater or equal to $z$

6. If step [5] is true, then print $y$ is the largest

7. If step [5] is false then print $z$ is the largest




2. Pseudocode to find the largest among three numbers


Input: Three numbers $x$, $y$, $z$

Output: Largest among $x$, $y$, $z$

1. Procedure findLargest($x$, $y$, $z$):

2. If $x \geq y$:

3. If $x \geq z$:

4. Return $x$

5. Else:

6. Return $z$

7. Else:

8. If $y \geq z$:

9. Return $y$

10. Else:

11. Return $z$

12. End Procedure





3. Time Complexity to find the largest among three numbers


Time Complexity: O(1)





4. C Program to find the largest among three numbers

Code has been copied
/**********************************************
            alphabetacoder.com
 C program to find largest among three numbers
***********************************************/

#include <stdio.h>

int main() {
    // declare variables
    int x, y, z;

    //take input of three numbers
    printf("Enter the three numbers = ");
    scanf("%d%d%d", & x, & y, & z);

    // find the largest number
    if (x >= y) {
        if (x >= z)
            printf("%d is the largest number.", x);
        else
            printf("%d is the largest number.", z);
    } else {
        if (y >= z)
            printf("%d is the largest number.", y);
        else
            printf("%d is the largest number.", z);
    }

    return 0;
}

Output


Case 1:

Enter the three numbers = -3 -8 -23

-3 is the largest number.


Case 2:

Enter the three numbers = 9 3 6

9 is the largest number.