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:
- Ask the user to enter three integer values.
- Read the three integer values in num1, num2, and num3 (integer variables).
- Check if num1 is greater than num2.
- If true, then check if num1 is greater than num3.
- If true, then print ‘num1’ as the greatest number.
- If false, then print ‘num3’ as the greatest number.
- If false, then check if num2 is greater than num3.
- If true, then print ‘num2’ as the greatest number.
- If false, then print ‘num3’ as the greatest number.
FlowChart to find the largest of three numbers:
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:
- C program to add two integer 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