If Else Statement Programs With Explanation And Code
\\ If you have any query or questions on my post, then please comment below //
Example Of If Else Statement With Program:
Write a program to enter 2 numbers and check if addition of two numbers is greater than 10 or not. If addition of two numbers is greater than 10 then printed a message "Addition Result is greater than 10" and if addition of two numbers is less than 10 then print a message "Addition result is less than 10":
Solution: You see, our above program is having 2 statements:
First Part: if the addition of 2 numbers is greater than 10 then print message "Addition Result is greater than 10". This statement, we can also call true statement.
Second: if addition of two numbers is less than 10 then print a message "Addition result is less than 10". This statement, we can call false statement, So for this program, it's mandatory to use if else statement. Because here we have to check the 2 statement or we can check one statement also.
So the above program, we can write in 2 ways:
1. By Checking both the statements.
2. By Checking one statement.
So in first way have to check both the statements. We know in an if else statement we can check 2 statements, First for if statement and Second for else statement. So For if statement, we will check added number is greater than 10, If number is greater than 10 then print message. Second in else statement we check added number is less than 10 or not.
-----------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 2 Numbers:");
scanf("%d%d",&a,&b);
c=a+b;
if (c>10)
{
printf("Addition Result is greater than 10");
}
else(c<10) // We can also write else statement like this, Read in the next chapter.
{
printf("Addition Result is less than 10");
}
getch();
}
-------------------------------------------------------------------------------------------
Now in the second way we can check only one statement, whether addition of two numbers is greater than 10 or less than 10. Now I am writing a program for both the option.
First Way: By Checking Addition Of 2 Numbers is greater than 10 or not.
-----------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 2 Numbers:");
scanf("%d%d",&a,&b);
c=a+b;
if (c>10) // Print only if the addition of 2 numbers is greater than 10, otherwise print else block.
{
printf("Addition Result is greater than 10");
}
else
{
printf("Addition Result is less than 10");
}
getch();
}
-------------------------------------------------------------------------------------------
Second Way: By Checking Addition Of 2 Numbers is less than 10 or not.
-----------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 2 Numbers:");
scanf("%d%d",&a,&b);
c=a+b;
if (c<10) // Print only if the addition of 2 numbers is less than 10, otherwise print else block.
{
printf("Addition Result is less than 10");
}
else
{
printf("Addition Result is greater than 10");
}
getch();
}
-------------------------------------------------------------------------------------------
Click Here To Know How To Download Above Video Tutorials Or YouTube Videos Free