➤ If-else Statement in C
➤ Programs on if-else
➤ Switch Case in C
➤ Switch case Programs
➤ Conditional Operator
➤ While loop in C
➤ Do-while loop in C
➤ While vs do-while
➤ For loop in C
➤ Break keyword in C
➤ Continue keyword in C
➤ Break vs Exit in C
➤ Goto keyword in C
☕️ Flow Control Programs
➤ Largest in 3 Numbers
➤ Find Grade of student
➤ Find the absolute value
➤ Vowel or Consonant
➤ Leap Year Program
➤ Simple calculator in C
➤ Check Odd or Even
➤ Roots of Quadratic Equation
➤ Find Reverse of Number
➤ Factors of a number in C
➤ Generate Multiplication table
➤ Find Power of a Number
➤ Find GCD and LCM
➤ Find factorial of Number
➤ Count Number of Digits
➤ Sum of digits in Number
➤ Sum of N Natural Numbers
➤ Sum of Squares of Natural No.
➤ Find Sum of Odd Numbers
➤ Find the Sum of Series
➤ Find Fibonacci series in C
➤ Sum of the Fibonacci series
➤ Sum until enters +ve numbers
➤ Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
➤ Celsius to Fahrenheit
➤ Fahrenheit to Celsius
➤ Decimal ↔ Binary
➤ Decimal ↔ Octal
➤ Octal ↔ Binary in C
☕️ Number Programs in C
➤ Prime Number in C
➤ Strong Number in C
➤ Krishnamurthy Number
➤ Neon Number in C
➤ Palindrome number
➤ Perfect Number in C
➤ Armstrong Number
☕️ Pattern Programs in C
➤ Pattern programs in C
➤ Printing pattern using loops
➤ Floyd’s triangle Program
➤ Pascal Triangle Program
➤ Pyramid Star Pattern in C
➤ Diamond Pattern Programs
➤ Half Diamond pattern in C
➤ Print Diamond Pattern
➤ Hollow Diamond Pattern
➤ Diamond Pattern of Numbers
Here we will write a C program to find the largest of three numbers. To solve this problem if-else statement will be used.
Prerequisites for finding the largest number among three numbers:-
C program to find the largest of three numbers using nested if-else statements
#include<stdio.h>
int main()
{
float num1, num2, num3;
printf("Enter three numbers: ");
scanf("%f %f %f",&num1,&num2,&num3);
if(num1 >= num2)
{
if(num1 >= num3)
printf("Largest Number=%.2f\n",num1);
else
printf("Largest Number=%.2f\n",num3);
}
else
{
if(num2>=num3)
printf("Largest Number=%.2f\n",num2);
else
printf("Largest Number=%.2f\n",num3);
}
return 0;
}
Output:-
Enter three numbers: 12.5 10 5.9
Largest Number=12.50
Let num1, num2, and num3 are three variables that will store the value of three numbers. First, we compare num1 with num2, if num1 is greater than we will compare it with num3. Using this comparison we find num1 is the largest number or the num3. In the previous comparison, if num1 is less than num2 then the next comparison takes place with num2 and num3. Finally, we got the Largest Number among the three numbers.
Using AND (&&) operator
The above program also can be written as below program. The structure of nested if-else statements in the below program is different from the previous program. This method is a short form of the above method. Here we will use AND (&&) operator.
#include<stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d",&num1,&num2,&num3);
if(num1>=num2 && num1>=num3)
printf("Largest=%d\n",num1);
else if(num2>=num1 && num2>=num3)
printf("Largest=%d\n",num2);
else
printf("Largest=%d\n",num3);
return 0;
}
In this program, AND operator is used. When two numbers are greater than equal to the third number then we can say, that number is the biggest among them.
C program to find the largest of three numbers using if statements
#include<stdio.h>
int main()
{
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d %d %d",&num1,&num2,&num3);
max = num1;
if(num2>max) max = num2;
if(num3>max) max = num3;
printf("Largest number = %d", max);
return 0;
}
Output:-
Enter three numbers: 10 20 50
Largest number = 50
In this program, we assigned the first entered number to a variable max. Now, we will compare the value of max with the remaining two numbers. If the second number is bigger than the value of max, then the value of the second number will be assigned to the variable max. Similarly, the same logic applied to the third number. Finally, we got the largest number among them.
Largest among three numbers using the conditional operator
#include<stdio.h>
int main()
{
float a, b, c, max;
printf("Enter three numbers: ");
scanf("%f %f %f",&a, &b, &c);
max = (a>b && a>c)? a: (b>a && b>c)? b:c;
printf("Maximum number = %.2f",max);
return 0;
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!