Skip to content

Multiple If

More than one simple if. It is used when we want to check different independent conditions and execute different corresponding blocks based on their evaluation.

Write C Program to check whether the given number is positive,negative or equal to zero.
Example:
#include<stdio.h>

void main()
{
int a;

printf(“Enter any number:”);
scanf(“%d”,&a);

if(a==0)
{
printf(“Your number is Zero”);
}
if(a>0)
{
printf(“%d is positive”,a);
}
if(a<0)
{
printf(“%d is negative”,a);
}
printf(“\nThe End”);
}

Exercise)
Write algorithm, draw flowchart and write c program to accept any number out of 2,4,6, or 8 from the user into a variable and display a corresponding message.

#include<stdio.h>

void main()
{
int n;
printf(“Enter any number 2,4,6 or 8 only:”);
scanf(“%d”,&n);

if(n==2)
{
printf(“TWO”);
}
if(n==4)
{
printf(“FOUR”);
}
if(n==6)
{
printf(“SIX”);
}
if(n==8)
{
printf(“EIGHT”);
}
if(n!=2 && n!=4 && n!=6 && n!=8)
{
printf(“Invalid Input”);
}
printf(“\nThan Q”);
}

if…else
it is used when we want to execute one out the two different blocks. One block to be executed when a condition is returned true and other block to be executed when the same condition is returned false.

else is optional part of simple if.
there can be if without else
but there cannot be else without if.

#include<stdio.h>

void main()
{
int a,b;

printf(“Enter any two number:”);
scanf(“%d%d”,&a,&b);

if(a>b)
{
printf(“%d is greater than %d”,a,b);
}
else
{
printf(“%d is greater than %d”,b,a);
}
printf(“\nThe End”);

}

Practice Questions)
1. Write C program to determine the shape is rectangle or square based on user entered dimensions.
(Hint: len==bre)

2. Write C program to determine whether the user entered number is even or odd.
(Hint: n%2==0)

3. Write C Program to determine big number out of two user entered numbers.

4. Write C program to determine whether the given number is either greater than or equal to zero or less than zero
hint: x>=0

5. Write C program to display “Discount Applicable” if the purchase amount is more than Rs. 5000/- other wise display “No Discount”

6. Write C program to display Final Price (Actual Price-Discount Rs. 50) if Price is more than Rs. 5000/- other wise display Actual Price as Final Price.

7. Write C program to check whether user entered alphabet is uppercase or smaller case alphabet.