How to write a C program to find largest of two numbers using Else If Statement, Conditional Operator, and Switch Case.
C Program to Find Largest of Two Numbers using Else If Statement
This C program helps the user to enter two different values, and then it will find the highest number among them using Else If Statement
/* C Program to Find Largest of Two numbers */ #include <stdio.h> int main() { int a, b; printf("Please Enter Two different values\n"); scanf("%d %d", &a, &b); if(a > b) { printf("%d is Largest\n", a); } else if (b > a) { printf("%d is Largest\n", b); } else { printf("Both are Equal\n"); } return 0; }
ANALYSIS
In this Program to Find the Largest of Two Numbers in C example, the execution of Else If Statement is
- First, if condition checks whether a is greater than b. If this condition is True, then a is greater than b.
- The first Else if condition checks whether b is greater than a. If this condition is True, then b is greater than a.
- If the above two conditions fail, it means they are equal.
OUTPUT 1: Lets enter the values a = 40, and b = 50
OUTPUT 2: Lets enter the values a = 20, and b = 10
OUTPUT 3: Lets enter the values a = 50, and b = 50
C Program to Find Largest of Two Numbers using Conditional Operator
This C program helps the user to enter two different values. Next, it will find the largest number among those two numbers using Conditional Operator
/* C Program to Find Largest of Two numbers */ #include <stdio.h> int main() { int a, b, largest; printf("Please Enter Two Different Values\n"); scanf("%d %d", &a, &b); if(a == b) { printf("Both are Equal\n"); } largest = (a > b) ? a : b; printf("%d is Largest\n", largest); return 0; }
ANALYSIS
Within this C Program to Find the Largest of Two Numbers example,
- First, We declared three integer variables called a, b, and largest. Next, we are allowing a user to enter their own values for a, and b
- First C Programming If condition checks whether a is equal to b or not. If this condition is True, then it will return both are Equal.
- Next, we used the Conditional Operator to check whether a is greater than b or not. If this condition is True, then it will return the first value after the ? Symbol, which is variable a (a is greater than b). If the first condition fails, then it will return the value after the : symbol, which is variable b.
OUTPUT 1: For this c largest of two numbers demo, Let’s enter the values a = 15, and b = 25
OUTPUT 2: Lets enter the values a = 35, and b = 12
OUTPUT 3: Lets enter the values a = 25, and b = 25
C Program to Find Largest of Two Numbers using Switch Case
This program helps the user to enter two different values. And then, this c program find the largest of two numbers using Switch Case
/* C Program to Find Largest of Two numbers */ #include <stdio.h> int main() { int a, b, largest; printf("Please Enter Two Different Values\n"); scanf("%d %d", &a, &b); switch(a > b) { case 1: printf("%d is Largest", a); break; case 2: printf("%d is Largest", b); break; } return 0; }
ANALYSIS
Within this C Program to Find Largest of Two Numbers example
- We declared three integer variables called a, b, and largest. Next, we are allowing the user to enter their own values for a, and b
- Next, we are using the Switch Case to check for the largest.
OUTPUT 1: Lets enter the values a = 25, and b = 56
OUTPUT 2: Lets enter the values a = 13, and b = 5