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
- Ask the user to enter two integer values.
- Read the two integer values in num1 and num2 (integer variables).
- Check if num1 is greater than num2.
- If true, then print ‘num1’ as the greatest number.
- If false, then print ‘num2’ as the greatest number.
FlowChart to find largest of two given numbers:
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:
- C program to find the largest of three given numbers
- C program to find the factorial of a number
- C Program to swap two nibbles in a byte
- C Program to find first and last digit of a given number
- C program to find even and odd numbers
- C Program to find the sum of natural numbers up to n terms
- C Program to find the sum of even natural numbers from 1 to n
- C Program to find the sum of odd natural numbers from 1 to n
- C Program to find if given number is sum of first n natural numbers
- C program to find sum of first and last digit of a Number
- C program to find the sum of digits of a number
- C program to check leap year
- C program to check valid date (date is valid or not)
- C program to Check expiry date
- C program to find reverse of a number using recursion
- C program to reverse digits of an integer with overflow handled