CPP All : Multiple Nested Ladder of If Else
Concept Learning Code Sheets

#### If Else If – Simple If Ladder #1914

Simple if-else-if in a ladder style comparison

#include <iostream>
using namespace std;
int main( )
{
int a,b;
cout<<"Enter integer a:";cin>>a;
cout<<"Enter integer b:";cin>>b;
if(a==b)
cout<<"A is equal to B"<<endl;
else if(a>b)
cout<<"A is greater than B"<<endl;
else
cout<<"A is smaller than B"<<endl;
return 0 ;
}

#### If Else If – Age of Friend problem bettered #1917

Simple if-else-if in a ladder style comparison to resolve age of friends problem which was not able to compare two friends with equal age.

#include <iostream>
using namespace std;
int main( )
{
int sa,fa;
cout<<"Enter your friend's age : ";cin>>fa;
if(sa==fa)
cout<<"You both friends are equal in age."<<endl;
else if(sa>fa)
cout<<"You are older than your friend."<<endl;
else
cout<<"You friend is older than you."<<endl;
return 0 ;
}

#### Volume purchase discount – multiple levels #1922

Calculating volume discounts on purchase quantity volumes based on multiple levels of quantity slabs.

#include <iostream.h>
using namespace std;
int main()
{
int vol,discount;
cout<<"Enter purchase volume :";
cin >> vol;
if (vol >=1000) discount=30;
else if (vol <1000 && vol >=500) discount=20;
else if (vol <500 && vol >=100) discount=10;
else discount=0;
return 0;
}

#### Marks Based Division #1924

Finding division of a student based on marks using if-else-if ladder

#include <iostream.h>
using namespace std;
int main()
{
const int MM = 50;
float marks,per ;
cout << "Enter your marks "<<"- out of "<<MM<<":";
cin >> marks;
per = marks/MM*100;
if (per >=60) cout << "You Got Ist Div.";
else if (per <60 && per >=45) cout << "You Got IInd Div.";
else if (per <45 && per >=33) cout << "You Got IIIrd Div.";
else cout << "Sorry! you are fail!";
return 0;
}

#### Electricity Bill – if-else-if – successive slab-wise rates #1929

Classic case of successive slab wise rates calculation for electricity bill generation solved using if-else-if and slab rate formulas.

#include <iostream>
using namespace std;
int main( )
{
int units;
float rate1=4.5,rate2=5.5,rate3=6.5,rate4=7.0;
float bill;
cout<<"Enter unit consumed : ";cin>>units;
if(units<=200) bill=units*rate1;
else if(units>200 && units<=400)
bill=(units-200)*rate2+
200*rate1;
else if(units>400 && units<=600)
bill=(units-400)*rate3+
200*rate2+200*rate1;
else
bill=(units-600)*rate4+
200*rate3+200*rate2+200*rate1;
return 0 ;
}

#### Check character entry as digit, uppercase, lowercase, special #1931

Finding if the character entered by the user is a digit, upper case alphabet, lower case alphabet or other special character using if-else-if ladder.

#include<iostream.h>
using namespace std;
int main( )
{
char ch ;
cout << "Enter a character : " ;
cin >> ch ;
if(ch >= '0' && ch <= '9')
cout << "You entered a digit " ;
else if(ch >= 'A' && ch <= 'Z')
cout << "You entered an upper case character " ;
else if(ch >= 97 && ch <= 122)
cout << "You entered a lower case character " ;
else
cout << "You entered a special character " ;
return 0 ;
}

#### Age and gender discount – Nested if condition #1937

Solving for more than one if conditions using nested if for conditions on age and gender of person

#include <iostream.h>
using namespace std;
int main()
{
int age,disc=0;char gender; float tkt;
cout<<"Enter your age :";cin >> age;
cout<<"Regular ticket price :";cin>>tkt;
if(age>=60)
{
if (gender =='F') disc=20;
else disc=10;
}
cout<<"Your regular ticket amount = "<<tkt<<endl;
cout<<"Your final ticket amount = "
<<tkt-tkt*disc/100<<endl;
return 0;
}

#### Territory-wise volume based sales commission – nested if #1945

Finding the volume based sales commissions based on multiple territories using the nesting of if conditions

#include <iostream.h>
using namespace std;
int main()
{
int vol,comm,tt;
cout<<"Enter territory number (1/2) :";cin>>tt;
cout<<"Enter sales volume :";cin >> vol;
if(tt==1)
{
if (vol >=1000) comm=10;
else if (vol <1000 && vol >=500) comm=5;
else comm=0;
}
else if (tt==2)
{
if (vol >=1500) comm=10;
else if (vol <1500 && vol >=700) comm=5;
else comm=0;
}
else comm=0;
cout<<"The sales commission would be "<<comm<<"%"<<endl;
return 0;
}

#### Multiple if conditions #1947

Using multiple if conditions which are often redundant in nature.

#include <iostream>
using namespace std;
int main( )
{
int a,b;
cout<<"Enter integer a:";cin>>a;
cout<<"Enter integer b:";cin>>b;
if(a==b)
cout<<"A is equal to B"<<endl;
if(a>b)
cout<<"A is greater than B"<<endl;
if(a<b)
cout<<"A is smaller than B"<<endl;
return 0 ;
}

#### Body temperature diagnosis and diversion – Multiple if and if ladder #1950

Handling of patients diversion to doctors based on there body temperatures using multiple if and if ladder.

#include <iostream>
using namespace std;
int main( )
{
float body_temp;
cout<<"Enter body temp:";cin>>body_temp;
if(body_temp<=98.6 && body_temp>=97)
{
cout<<"You are okay"<<endl;
return 0;
}
if(body_temp>103 && body_temp<110)
cout<<"Go to Emergency Ward"<<endl;
else if(body_temp<=103 && body_temp>100)
cout<<"Go to Doctor X"<<endl;
else if(body_temp<=100 && body_temp>98.6)
cout<<"Go to Doctor Y"<<endl;
else
cout<<"Unable to handle input value"<<endl;
return 0 ;
}

#### Userid and password verification – nested if #1953

Learning to verrify user id and password using nested if selection

#include <iostream>
using namespace std;
int main( )
{
int id1=123,pw1=321;
int id,pw;
cout<<"Enter userid : ";cin>>id;
if(id==id1)
{
if(pw==pw1)
cout<<"Access Granted"<<endl;
else
}
else
return 0 ;
}

#### Userid, password match with dangling else issue #1959

Userid and password matching with nested if and the dangling else issues.

#include <iostream>
using namespace std;
int main( )
{
int id1=123,pw1=321;
int id,pw;
cout<<"Enter userid : ";cin>>id;
if(id==id1)
if(pw==pw1)
cout<<"Access Granted"<<endl;
else
return 0 ;
}

Solved Problems

#### Leap year finding #2008

Write a c++ program to take the value of a four digit year from the user and

#### Largest of three numbers using if else ladder #2946

Write a c++ program to find the largest of three numbers using the if-else se

Code Sheets:12  Solved Problems:2
×