C Program To Find The Largest Number in Two Variables

Hey Shouters !! Today we will learn about C Program to Find the Largest Number in Two variables. There are two methods to find the Largest number in two variables.
- Using Relational Operator (>)
- Using Conditional Operator (?:)
The C Program with Conditional Operator is very important and is asked in many big companies. So, it becomes priority to understand the program.
1. C Program To find the Largest Number – Using Relational Operator
In this program we will use if-else statement to check that which number is the largest number. Suppose we have a =20 and b =30. We will add the condition in the if statement, if that statement is correct we will get the output and if that is wrong then else part will be executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// C Program to find the largest number #include <stdio.h> #include<conio.h> int main() { int a =20; int b =30; // Declaration and definition printf("Comparing the variables \n"); if(a>b) // comparing the variables { printf(" a is the largest numbers%d",a); } else { printf("b is the largest number %d",b); } } |
1 2 3 |
<strong>Output - </strong> Comparing the variables b is the largest number 30 |
So, in the above program the statement in the if part is wrong that 20>30. So, else part will be executed and we will get the output.
C Program To Find the Largest Number- Using Conditional Operator
The approach is very simple and does not need to write lengthy code. In this we will just write one line and our problem will be solved. Suppose we have a= 55 and b =45. We will use Conditional operator, if the statement is correct, the first task will be executed otherwise second one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// C Program to find the largest number #include <stdio.h> #include<conio.h> int main() { int a =55; int b =45; // Declaration and definition printf("Comparing the variables \n"); // Usimg conditional operator (a > b) ? printf("a is the largest number %d",a) : printf("b is the largest number%d",b); } |
1 2 3 |
<strong>Output -</strong> Comparing the variables a is the largest number 55 |
We daily add Multiple Choice Questions for placement preparation questions and technical interview preparation on the Instagram page. Check the Instagram account: Shout Coders
Frequently Asked Questions –
Yes, it is very important to understand the Conditional operator.
Yes, the second method will be asked but only in product based companies.