codescracker
c

C Program to Find Largest among Three Numbers



« Previous Program Next Program »

In this tutorial, you will learn and get about how to find largest or greatest number among given three numbers by user (at run-time) with and without using ternary operator. Here are the approaches you will go through:

Find Largest of Three Numbers using if-else-if

To find the largest number among given three numbers in C programming, you have to ask from the user to enter any three number, now start checking which one is the largest using if-else block as shown in the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    int a, b, c, big;
    printf("Enter three numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    if(a>b)
    {
        if(b>c)
            big = a;
        else
        {
            if(c>a)
                big = c;
            else
                big = a;
        }
    }
    else
    {
        if(b>c)
            big = b;
        else
            big = c;
    }
    printf("\nLargest number = %d", big);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, you will get the following output:

c program find biggest of three numbers

Supply any three numbers say 3, 1, and 2. Press ENTER key to see the largest number as given in the second snapshot of the sample run:

c program find largest of three numbers

Here is another sample run:

c program find greatest in three numbers

And here is the last sample run of above program:

c print largest of three numbers

Here are the cases, that user can enter the three numbers:

Program Explained

Find Largest of/among Three Numbers using if-else Statement

This program also does the same job but in a simple manner, using if-else statement of C language:

#include<stdio.h>
#include<conio.h>
int main()
{
    int a, b, c, big;
    printf("Enter three numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    if(a>b && a>c)
        big = a;
    else if(b>a && b>c)
        big = b;
    else
        big = c;
    printf("\nLargest number = %d", big);
    getch();
    return 0;
}

The above program is little simple to create and understand in comparison to the above one. You will see the same output as of previous one.

Find Largest of Three Numbers using Ternary Operator

Again I'm going to create the same program using ternary operator (?:)

#include<stdio.h>
#include<conio.h>
int main()
{
    int num1, num2, num3, large;
    printf("Enter any three numbers: ");
    scanf("%d%d%d", &num1, &num2, &num3);
    large = (num1>num2) ? ((num1>num3)?num1:num3) : ((num2>num3)?num2:num3);
    printf("Largest/Maximum number = %d", large);
    getch();
    return 0;
}

After successfully build and run the above program, here is the first screenshot of the sample run:

largest of three number ternary operator

Now supply any three numbers as input say 12, 23, and 11 and then press ENTER to see the following output:

largest of three using ternary operator c

Program Explained

Here we have used nested ternary operator, that is ternary operator inside the ternary operator.

Find Greatest among Three Numbers using Function

This is another program doing the same job as of all the previous programs does. The only difference with previous programs is, it is created using a user-defined function findLarge().

#include<stdio.h>
#include<conio.h>
int findLarge(int, int, int);
int main()
{
    int a, b, c, big;
    printf("Enter three numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    big = findLarge(a, b, c);
    printf("\nLargest number = %d", big);
    getch();
    return 0;
}
int findLarge(int num1, int num2, int num3)
{
    if(num1>num2)
    {
        if(num2>num3)
            return num1;
        else
        {
            if(num3>num1)
                return num3;
            else
                return num1;
        }
    }
    else
    {
        if(num2>num3)
            return num2;
        else
            return num3;
    }
}

Here is the final snapshot of sample run:

c find largest of three using function

Here the function takes the three given numbers as its argument and will return the largest one that will be initialized to the variable say big inside the main() function. Print the value of big, that will hold the largest value among the given three numbers. To learn more about function, then you can follow separate tutorial on it.

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker