C program to find largest of two given numbers

C program to find largest of two given numbers

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

Example,

Input: num1 = 2, num1 = 8
Output: Largest number = 8


Input: num1 = 20, num1 = 18
Output: Largest number = 20

Algorithm to find the greatest of two numbers

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

 

FlowChart to find largest of two given numbers:

Find Largest Of Two Number In C

 

Methods to Find the Largest of Two Numbers:

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

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

 

Find the largest of two numbers using the if-else condition:

In the below example code, I have used if-else statement to find the largest number of given two numbers.

 

#include <stdio.h>

int main()
{
    int num1, num2;

    // Ask user to enter the two numbers
    printf("Please Enter Two different values\n");

    // Read two numbers from the user
    scanf("%d %d", &num1, &num2);

    if(num1 > num2)
    {
        printf("%d is Largest\n", num1);
    }
    else if (num2 > num1)
    {
        printf("%d is Largest\n", num2);
    }
    else
    {
        printf("Both are Equal\n");
    }

    return 0;
}

Output:

Please Enter Two different values: 27  6
27 is Largest

 

Watch this video to see how to find the largest of two given numbers using the C program.

 

 

Find the largest of two given number using ternary condition:

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

#include <stdio.h>
int main()
{
    int num1, num2;

    // Ask user to enter the two numbers
    printf("Please Enter Two different values\n");
    // Read two numbers from the user
    scanf("%d %d", &num1, &num2);

    (num1 >= num2)?((num1 ==num2)?
                    printf("Both numbers are equal"):
                    printf("%d is Largest\n", num1)):
    printf("%d is Largest\n", num2);
    
    return 0;
}

Output:

Please Enter Two different values 6  6
Both numbers are equal

 

Using macros:

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

// C Code to Find the Largest Number Among Three using
// C macros
#include <stdio.h>
#define findLargest(a, b) ((a) > (b) ? (a) : (b))

int main()
{
    int num1, num2;

    // Ask user to enter the two numbers
    printf("Please Enter Two different values\n");

    // Read two numbers from the user
    scanf("%d %d", &num1, &num2);

    const int largestNum = findLargest(num1, num2);

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

    return 0;
}

Output:

Please Enter Two different values -3 4
4 is Largest.

 

Using Bit-wise Operator:

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

// C Code to Find the Largest Number Among Three using
//bit-wise operator
#include <stdio.h>

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

    return tmp;
}

int main()
{
    int num1, num2;
    printf("Please Enter two different values\n");

    scanf("%d %d", &num1, &num2);

    const int largestNum = findLargest(num1, num2);

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

    return 0;
}

Output:

Please Enter Two different values -3 4
4 is Largest.

 

Recommended Post for you: