AND Operator in If Statement in C

The logical AND operator In C programming represents '&&' double ampersand. The AND operator will check two or more conditions by combining them in an expression.

Program using AND Operator in If Statement

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter value from 1 to 5: ");
scanf("%d", &num); //user enter value storing in 'num'.
if(num >= 1 && num <= 5) //if 'num' value Less than or equal to 1 And
{                          //greater then OR equal to 5 then
printf("Welcome!"); //print this, and U also write like
}                    //'if((num >= 1) && (num <= 5))'.
else //if not then
{
printf("Invalid Entry!"); //print this.
}
getch();
}
Output
Enter value from 1 to 5: 4
Welcome!

Program using OR Operator in If Statement

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter value from 1 to 4: ");
scanf("%d", &num); //user enter value storing in 'num'.
if(num == 1 || num == 2 || num == 3 || num == 4) //if 'num' value equal to 1
{ //OR equal to 2 OR equal to 3 OR equal to 4 then
printf("Welcome!"); //print this.
}
else //if not then
{
printf("Invalid Entry!"); //print this.
}
getch();
}
Output
Enter value from 1 to 4: 2
Welcome!

Program Calculator in If Statement

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
char op;
printf("Enter First Integer: ");
scanf("%d", &a);
printf("Enter Operator: ");
scanf("%s", &op); //user enter operator storing in 'op'.
printf("Enter 2nd Integer: ");
scanf("%d", &b);
if(op == '+') //if 'op' value equal to '+' then
{
printf("Addition is: %d", a+b); //print this.
}
else if(op == '-') //if 'op' value equal to '-' then
{
printf("Subtraction is: %d", a-b); //print this.
}
else if(op == '*') //if 'op' value equal to '*' then
{
printf("Multiplication is: %d", a*b); //print this.
}
else //if not in above then
{
printf("Invalid Operator"); //print this.
}
getch();
}
Output
Enter First Integer: 5
Enter Operator: *
Enter 2nd Integer: 6
Multiplication is: 30