C program to find largest of three given numbers

C program to find largest of three given numbers is discussed here. Input three integers from the user and find the largest number among them. Given three numbers num1,num2, and num3. The task is to find the largest number among the three.

Example,

Input: num1= 2, num2 = 18, num3= 10
Output: Largest number = 18


Input: num1= 20, num2 = 18, num3= 100
Output: Largest number = 100

 

 

Algorithm to find greatest number of three given numbers:

  1.  Ask the user to enter three integer values.
  2. Read the three integer values in num1, num2, and num3 (integer variables).
  3. Check if num1 is greater than num2.
  4. If true, then check if num1 is greater than num3.
    1. If true, then print ‘num1’ as the greatest number.
    2. If false, then print ‘num3’ as the greatest number.
  5. If false, then check if num2 is greater than num3.
    1. If true, then print ‘num2’ as the greatest number.
    2. If false, then print ‘num3’ as the greatest number.

 

FlowChart to find the largest of three numbers:

Find largest of three given numbers in c

 

Methods to Find the Largest of Three Numbers:

  • Using if Statement.
  • Using nested if-else Statement.
  • Using if-else Statement.
  • Using ternary operator.
  • Using temporary variable.
  • Using macros.
  • Using Bit-wise Operator.

Let’s discuss each of them to find the largest of the three numbers with the help of C programs.

Using the if condition:

In the below example code, I have used only if statements to find the largest number of given three numbers.

#include <stdio.h>


int main()
{
    int num1, num2, num3;
    printf(" Enter the number1 = ");
    scanf("%d", &num1);
    printf("\n Enter the number2 = ");
    scanf("%d", &num2);
    printf("\n Enter the number3 = ");
    scanf("%d", &num3);


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

    return 0;
}

Output:

Enter the number1 = 6

Enter the number2 = 27

Enter the number3 = 24

27 is the largest number.

 

Using the nested if-else statement:

In the below example code I have used nested if-else statements to find the largest number of given three numbers.

#include <stdio.h>

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

    //Get and store value in num1
    printf(" Enter the number1 = ");
    scanf("%d", &num1);

    //Get and store value in num2
    printf("\n Enter the number2 = ");
    scanf("%d", &num2);

    //Get and store value in num3
    printf("\n Enter the number3 = ");
    scanf("%d", &num3);


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

    return 0;
}

Output:

Enter the number1 = 12

Enter the number2 = 98

Enter the number3 = 1

98 is the largest number.

 

 

Using the if-else Ladder:

In the below example code I have used if-else ladder statements to find the largest number of given three numbers.

#include <stdio.h>

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

    printf(" Enter the number1 = ");
    scanf("%d", &num1);

    printf("\n Enter the number2 = ");
    scanf("%d", &num2);

    printf("\n Enter the number3 = ");
    scanf("%d", &num3);

    if (num1 > num2)
    {
        if (num1 > num3)
        {
            printf("\n Largest number = %d \n",num1);
        }
        else
        {
            printf("\n Largest number = %d \n",num3);
        }
    }
    else if (num2 > num3)
    {
        printf("\n Largest number = %d \n",num2);
    }
    else
    {
        printf("\n Largest number = %d \n",num3);
    }
    return 0;
}

Output:

Enter the number1 = 12
Enter the number2 = 62
Enter the number3 = 27
Largest number = 62

 

 

Using ternary conditions:

In the below example code, I have used ternary operators to find the largest number of given three numbers.

#include <stdio.h>

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

    printf("Please Enter three different values\n");
    scanf("%d %d %d", &num1, &num2, &num3);

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

    printf("Largest number = %d \n",largest);

    return 0;
}

Output:

Please Enter three different values: 23  56  2
Largest number = 56

 

Using temporary variable:

In the below C code, I have used temporary variable and if statement to find the largest number of given three numbers.

// C Code to Find the Largest Number Among Three using
// Temporary Variable

#include <stdio.h>

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

    printf("Please Enter three different values\n");
    scanf("%d %d %d", &num1, &num2, &num3);

    // temporary varaible
    int largest = num1;

    if (largest < num2)
    {
        largest = num2;
    }
    if (largest < num3)
    {
        largest = num3;
    }

    printf("largest among %d, %d, and %d is: %d", num1, num2,num3,
           largest);

    return 0;
}

Output:

Please Enter three different values: -3 -567  2

Largest number among -3, -567, and 2 is: 2

 

Using macros:

In the below C code, I have used C macros to find the largest number of given three numbers.

// C Code to Find the Largest Number Among Three using
// C macros

#include <stdio.h>

#define max(a, b) ((a) > (b) ? (a) : (b))
#define findLargest(a, b, c) max(max(a, b), c)

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

    printf("Please Enter three different values\n");
    scanf("%d %d %d", &num1, &num2, &num3);

    const int largestNum = findLargest(num1, num2, num3);

    printf("Largest number among %d, %d, and %d is: %d\n",\
           num1,num2,num3, largestNum);

    return 0;
}

Output:

Please Enter three different values: -3 -567  1

Largest number among -3, -567, and 1 is: 1

 

Using Bit-wise Operator:

In the below C code, I have used bit-wise operator to find the largest number of given three numbers.

// C Code to Find the Largest Number Among Three using
//bit-wise operator

#include <stdio.h>

int findLargest(int x, int y, int z)
{
    int tmp   =  x ^ ((x ^ y) & -(x < y)); // max(x, y)
    tmp = tmp ^ ((tmp ^ z) & -(tmp < z)); // max(tmp,z)
    return tmp;
}

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

    printf("Please Enter three different values\n");
    scanf("%d %d %d", &num1, &num2, &num3);

    const int largestNum = findLargest(num1, num2, num3);

    printf("Largest number among %d, %d, and %d is: %d\n",\
           num1,num2,num3, largestNum);
    return 0;
}



 

 

Recommended Post for you:

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *