5.7 – Nested if else statement

by subbu on September 16, 2013

Nested if else selection statement:

Some times we need to check one or more conditions on satisfying or unsatisfying other conditions. In such case we need to place a selection statement in another selection statement called nesting of selection statements. There are no global rules to place a statement in another statement. We need to craft and test carefully by giving different inputs to make sure that the program functions properly. It gives additional flexibility to the problem solving.

Now we will see, why do we need nested if else selective statements to solve some problems. I believe in “learning through programs”. So let us solve some problems!

1) Accept two numbers and print the biggest number.

Logic:
First we need to check for equality, if so then print “Equals” and come out otherwise check for biggest number again.

Using if selective statement

  if(a==b)
    printf("Equals");
  if(a>b)
    printf("Biggest number %d",a);
  if(b>a)
    printf("Biggest number %d",b);
/* using nested if else */
#include<stdio.h>
int main()
{
int a,b;
printf("Enter two integers:\n");
scanf("%d%d",&a,&b);
if(a==b)
  printf("Equals");
else
{
    if(a>b)
      printf("Biggest number %d",a);
    else
      printf("Biggest number %d",b);
}
return 0;
}

Execution 1:
Enter two integers:
25      25
Equals

Execution 2:
Enter two numbers:
67      45
Biggest number 67

2) Accept any number and print whether it is zero, positive or negative

using if selective statement:

  if(n==0)
    printf("Zero");
  if(n>0)
    printf("Positive");
  if(n<0)
    printf("Negative");
/* using nested if else */
#include<stdio.h>
int main()
{
float n;
printf("Enter any number:");
scanf("%f",&n);
if(n==0)
  printf("Zero");
else
{
    if(n>0)
       printf("Positive number");
    else
       printf("Negative number");
}
return 0;
}

3) Accept the cost price (purchase price), sold price and print whether seller got profit or loss, if so how much in percentage?

Logic:
Percentage of profit = (Sales price – Purchase price)*100/Purchase price
Percentage of loss   = (Purchase price- Sales price)*100/Sales price

#include<stdio.h>
int main()
{
float pprice,sprice;
printf("Enter the purchase price:");
scanf("%f",&pprice);
printf("Enter the sales price:");
scanf("%f",&sprice);
if(pprice==sprice)
   printf("No profit no loss ");
else
{
   if(sprice>pprice)
      printf("Profit of %f percent",(sprice-pprice)/pprice*100);
   else
      printf("Loss of %f percent",(pprice-sprice)/pprice*100);
}
return 0;
}

Execution 1:
Enter the purchase price:100
Enter the sales price:200
Profit of 100.00 percent

Execution 2:
Enter the purchase price:35
Enter the sales price:15
Loss of  57.14  percent

4) Accept any year and print whether the year is a leap year or not?

Logic:
If the year is a century year then it must be divisible by 400
If the year is a not a century year then it must be divisible by 4

#include<stdio.h>
int main()
{
int year;
printf("Enter the year:");
scanf("%d",&year);
if(year%100==0)
{
    if(year%400==0)
       printf("Leap year");
    else
       printf("Not a leap year");
}
else
{
    if(year%4==0)
       printf("Leap year");
    else
       printf("Not a leap year");
}
return 0;
}

Execution 1:
Enter the year:1996
Leap year

Execution 2:
Enter the year:2000
Leap year

Execution 3:
Enter the year:1900
Not a leap year

5) Accept any integer and print whether a single digit number or not.

#include<stdio.h>
int main()
{
int n;
printf("Enter any integer:");
scanf("%d",&n);
if(n>=-9)
{
   if(n<=9)
      printf("Single digit number");
   else
      printf("Not a single digit number");
}
else
   printf("Not a single digit number");
return 0;
}

Execution 1:
Enter any integer: -25
Not a single digit number

Execution 2:
Enter any integer: 0
Single digit number

Execution 3:
Enter any integer: 100
Not a single digit number

Previous post:

Next post: