if-else statement in C ?


The if-else statement is a simple selection/decision statement that is used when we must choose between two alternatives. Hence, it is also called two-way selection decision/selection statement.


Note: "To overcome the drawbacks of simple if statement/one way selection we use two way selection statement known as if-else statement."

Syntax:-


if(expression)
{
statement f1
statement f2
statement..fn
}
the statement f1 to fn is executed when this condition is true.
else
{
statement t1
statement t2
statement..tn
}
the statement is executed when the above if condition is not true.



Q.) W.A.P to print the greater number using simple if-else statement.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int a, b;
 printf(“Enter two No.”);                   
 scanf(“%d%d”,&a,&b);
 if(a>b)
 {
 printf(“ A is greater No.”);
 }
 else
 {
  printf(“B is greater No.”);
 }
 getch();
 }
Output
enter two numbers:3
4
B is greater no
Program Explanation

Step 1:-
We declared two integer variables called a and b.
Step 2:-
A Message will give for entering their own values for a and b:- Enter Two No
Step 3:-
User Enter two integer No Let`s We Enter :- 6 3
Step 4:-
next If condition checks whether a is greater than b or not. If this condition is True, then it will return a is greater than b. if the condition is not true then it will return a is not greater than b
hear we used the Conditional Operator(i.e > greater than) to check whether a is greater than b or not. If this condition is True, then it will be executed if part, which is variable an (a is greater than b). If the first condition fails, then it will be executed else part, which will give output as b is greater than a
Step 5:-
in this program, the condition is true because a is greater than b os if a part will be executed because we have enter 6 and 3 i.e 6 is greater than 3
if we enter 3 and 6 then 3 is less than 6 so else part will be executed
Step 6:-
So Output will be :- a is grater than b

Q.) W.A.P to Enter Two no and Check both are Equals or not.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int a, b;
 printf(“Enter two No.”);                   
 scanf(“%d%d”,&a,&b);
 if(a==b)
 {
 printf(“ Both are Equals”);
 }
 else
 {
  printf(“Both Are not Equals”);
 }
 getch();
 }
Output
enter two numbers:3 4
Both are not Equals

Program Explanation

Step 1:-
We declared two integer variables called a and b.
Step 2:-
Message will give for enter their own values for a and b:- Enter Two No.
Step 3:-
User Enter two integer No Let`s We Enter :- 4 5
Step 4:-
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. if the condition is false then it will be executed else part
hear we used the Conditional Operator(i.e equals to ==) to check whether a is equals to b or not. If this condition is True, then it will be executed if part, which is variable a (a is equal to b). If the first condition fails, then it will execute else part, which will give output as a is not equal to b
Step 5:-
Hear,else part will be executed because we have enter 4 and 5 i.e both are not equals
if we enter 4 and 4 than both numbers are equals so it will give output as both numbers are equals Step 6:-
So Output will be :- both are not equls

Q.) W.A.P to Enter a no and check Even or Odd
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int n;
 printf(“Enter a no”);                   
 scanf(“%d”,&n);
 if(n%2==0)
 {
 printf(“ Even”);
 }
 else
 {
  printf(“Odd”);
 }
 getch();
 }
Output
enter a numbers:4
Even

Program Explanation

Step 1:-
We declared a integer variables called n that will store number .
Step 2:-
Message will give for enter their own values for n:- Enter a No.
Step 3:-
User Enter a integer No Let`s We Enter :- 4
Step 4:-
as we know even numbers are divisible by 2 remaining of 0 is called an even number.
and we also know that for storing remainder we use % (modulus operator) next If condition checks whether n is divisible by 2 or not. If this condition is True, then it will return even else odd.

hear we used the Conditional Operator(==) and arithmetic operator (%) for store remainder to check whether the remainder is zero or not. If this condition is True, then it will be executed if part, which is even. If the first condition fails, then it will execute else part, which will give output as odd
Step 5:-
in this program, condition is true because 4 is even of if a part will be executed because we have enter 4 i.e 4 is even
if we enter 5 then output will be odd so else part will be executed
Step 6:-
So Output will be :- even


Q.) W.A.P to Enter a no and check divisible by 3 or not.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int n;
 printf(“Enter a no”);                   
 scanf(“%d%d”,&n);
 if(n%3==0)
 {
 printf(“ Divisible by 3”);
 }
 else
 {
  printf(“not divisible by 3”);
 }
 getch();
 }
Output
enter a numbers:15
divisible by 3

Program Explanation

Step 1:-
We declared a integer variables called n that will store number .
Step 2:-
Message will give for enter their own values for n:- Enter a No.
Step 3:-
User Enter a integer No Let`s We Enter :- 15
Step 4:-
we know that for storing remainder we use % (modules operator) next If condition checks whether n is divisible by 3 or not. If this condition is True, then it will return divisible by 3 else not divisible by 3.

hear we used the Conditional Operator(==) and arithmetic operator (%) for store remainder to check whether the remainder is zero or not. If this condition is True, then it will be executed if part, which is divisible by 3. If the first condition fails, then it will execute else part, which will give output as not divisible by 3
Step 5:-
in this program, condition is true because 15 is divisible by 3 if part will be executed because we have entered 15 i.e 15 is divisible by 3
if we enter 16 then output will be not divisible by 3 so else part will be executed
Step 6:-
So Output will be :- divisible by 3