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.

 

Below is the C program to find the largest among the two numbers:

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

 

#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

 

Find the largest of two given number using ternary condition:

 

#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