C Program To Compare two variables

Hey Shouters !! Today we will understand C Program to Compare two variables. We can compare two variable to find some relation between them.
Some of the popular Relations are :
- To find the largest number
- To find the smallest number
- Check that the variables are equal or not.
1. C Program to find the largest number-
If you have two variables suppose a=10 and b =20. To find the largest number we have to compare the two variables using the relational operator greater than (>) to get the largest number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// C Program to find the largest number #include <stdio.h> #include<conio.h> int main() { int a =10; int b =20; // 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 20 |
2. C Program to find the smallest number-
If you have two variables suppose a=20 and b =30. To find the smallest number we have to compare the two variables using the relational operator lesser than (<) to get the smallest number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// C Program to find the smallest 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 smallest numbers %d",a); } else { printf("b is the smallest number %d",b); } } |
1 2 3 |
Output - Comparing the variables a is the smallest number 20 |
3. C Program to check the variables are equal or not –
In this program, we will see how to check that the two variables are equal or not. We will use the relational operator double equals (==) to compare the variables.
Suppose a and b are two variables and a=25 and b =25. Then the if statement will work. and if a=25 and b =20 then else statement will be executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// C Program to check the variables are equal or not #include <stdio.h> #include<conio.h> int main() { int a =25; int b =25; // Declaration and definition printf("Comparing the variables \n"); if(a==b) // comparing the variables { printf(" a is equal to b and both are %d",a); } else { printf("The variables are not equal"); } } |
1 2 3 |
<strong>Output - </strong> Comparing the variables a is equal to b and both are 25 |
Now you can write a C Program to compare two variables to check other relations as well.
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, Conditional operator (?:) is used to compare the two variables in any relation.
Yes, we can use functions to compare the two variables.
This program is just for basic understanding and you have to understand this for solving tougher problems.
Hey Cool Stuff man