Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday 10 March 2022

23.Else-if ladder

 

else-if ladder is a control structure used to check multiple conditions out of which any single condition must be true.

In else-if ladder, condition by condition are checked, if any condition is true then statements under the conditional statement is executed and left all the structure is skipped from the execution.

If no conditions are true then the else part is executed.

Defining of else part is optional in else-if ladder.

Syntax:

 

if(Condition)

{

    ------------------

    ------------------

}

else if(Condition)

{

    -----------------

    -----------------

}

else if(Condition)

{

     ----------------

     ----------------

}

else

{

     ----------------

     ----------------

}

 

 
 


  

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

                                                                                   

                                                                                                 

 

 

 

                                                                                                                           

 

 

 

 

 

 

 

 

 

 

 

 

Note: there should be a space among the if and else.

 

Difference among the if and else-if ladder:

 


#include<stdio.h>
void main()
{
if(10<20 if="" ne="" printf="">80)
printf("\nTwo");
if(20!=40)
printf("\nFour");
}

Output:

One

Four

 


#include
void main()
{
if(10<20 else="" if="" ne="" printf="">80)
printf("\nTwo");
else if(20!=40)
printf("\nFour");
}

Output:

One

 

 

 

 

 

 

 

 

 

Specification1:

Accept any character from the keyboard and print whether the given character is a vowel, consonant, digit or special symbol.

 

Program:


#include<stdio.h>
void main()
{
char ch;
printf("Enter any character:");
scanf("%c",&ch);
if(ch==’a’ || ch== ‘e’ || ch==’i’ || ch==’o’ || ch==’u’)
printf("Vowel");
else if(ch>=’b’ && ch<=’z’)
printf("Consonant");
else if(ch>=’0’ && ch<=’9’)
printf("Digit");
else
printf("Special symbol");
}

Execution1:

Enter any character: e

Vowel

 

Execution2:

Enter any character: p

Consonant

 

Execution3:

Enter any character: 8

Digit

 

Execution4:

Enter any character: $

Special symbol

 

Specification2:

Accept the type of customer, principle amount, period of time and print the simple interest and the total amount must be paid to the financer.

Here the interest rates per month are 1% for employees, 2% for farmers, 3% for business people and 5 for others

      

Logic: S.I= P*T*R/100

 

Program:


#include<stdio.h>
void main()
{
long int amount;
int time,ch;
float si,total;
printf("1. Employee\n2. Farmer\n3. Business Person\n4. Other\nEnter your choice 1..4:");
scanf("%d",&ch);
printf("Principle Amount:");
scanf("%ld",&amount);
printf("Period of time in months:");
scanf("%d",&time);
if(ch==1)
si=(float)amount*time*1/100;
else if(ch==2)
si=(float)amount*time*2/100;
else if(ch==3)
si=(float)amount*time*3/100;
else
si=(float)amount*time*5/100;
total=amount+si;
printf("Amount %ld",amount);
printf("\nSimple Interest %f",si);
printf("\nTotal amount %f",total);
}

Execution:

1. Employee

2. Farmer

3. Business Person

4. Other

Enter your choice 1..4: 3

Principle Amount: 52500

Period of time in months: 5

Amount 52500

Simple interest 7875

Total amount 60375

 

 

 

 

Specification3:

Enter any number and print whether the given number is a single digit, two digit, three digit, biggest or smallest number.

 

#include<stdio.h>
void main()
{
Incase of simple if
if(n>=-9 && n<=9)
printf("Sigle digit number");
if((n>=-99&&n<=10) || (n>=10&&n<=99))
printf("Two digit number");
if((n>=-999&&n<=-100) || (n>=100&&n<=999))
printf("Three digit number");
if(n>999)
printf("Biggest number");
if(n<-999 any="" d="" if="" int="" mallest="" n="" nter="" number:="" number="" printf="" scanf="">=-9 && n<=9)
printf("Single digit number");
else if(n>=-99 && n<=99)
printf("Two digit number");
else if(n>=-999 && n<=999)
printf("Three digit number");
else if(n>999)
printf("Biggest number");
else
printf("Smallest number");
}
 

Execution1:

Enter any number: 25

Two digit number

 

Execution2:

Enter any number: -425

Three digit number

 

Execution3:

Enter any number: 1500

Biggest number

 

 

Specification4:

Accept the salary of an employee and print the HRA, DA and gross salary according to the following table.

 

Salary

HRA

DA

More than 25000

15%

55%

19001 to 25000

13%

42%

10001 to 19000

12%

35%

1 to 10000

5%

15%


Program:


#include<stdio.h>
void main()
{
long int sal;
float hra,da,gross;
printf("Enter the salary:");
scanf("%ld",&sal);
if(sal>25000)
{
hra=(float)sal*15/100;
da=(float)sal*55/100;
}
else if(sal>=19001)
{
hra=(float)sal*13/100;
da=(float)sal*42/100;
}
else if(sal>=10001)
{
hra=(float)sal*12/100;
da=(float)sal*35/100;
}
else
{
hra=(float)sal*5/100;
da=(float)sal*15/100;
}
gross=sal+hra+da;
printf("Salary details:\n");
printf("Salary %ld",sal);
printf("\n House Rent Allowance %f",hra);
printf("\n Dearness Allowance %f",da);
printf("\n Gross salary %f",gross);
}

Execution1:

Enter the salary: 21500

Salary details:

Salary 21500

House Rent Allowance 2795.000000

Dearness Allowance 9030.000000

Gross salary 33325

 

Specification5:

Accept the type of electrical connection, present month reading, previous month reading and print the number of units and bill according to the following table.

 

Type of connection

Units

Charge/unit

customer charges

Domestic

More than 500

6.25

45

301-500

6.00

45

201-300

4.75

40

101-200

3.05

35

51-100

2.80

30

0-50

1.45

25

Commercial

More than 100

6.50

40

51-100

6.20

35

0-50

3.85

30

Temples

More than 200

4.00

40

0-200

2.00

40

 

Program:



#include<stdio.h>
void main()
{
char type;
int cmr,pmr,nu,cch;
float bill;
printf("Enter the type of connection d/c/t:");
scanf("%c",type);
printf("Enter the current month meter reading:");
scanf("%d",&cmr);
printf("Enter the previous month meter reading:");
scanf("%d",&pmr);
nu=cmr-pmr;
if(type==’d’)
{
if(nu>=500)
{
bill=nu*6.25;
cch=45;
}
else if(nu>=301)
{
bill=nu*6.00;
cch=45;
}
else if(nu>=201)
{
bill=nu*4.75;
cch=40;
}
else if(nu>=101)
{
bill=nu*3.05;
cch=35;
}
else if(nu>=51)
{
bill=nu*2.80;
cch=30;
}
else
{
bill=nu*1.45;
cch=25;
}
}
else if(type==’c’)
{
if(nu>100)
{
bill=nu*6.50;
cch=40;
}
else if(nu>=51)
{
bill=nu*6.20;
cch=35;
}
else
{
bill=nu*3.85;
cch=30;
}
}
else if(type==’t’)
{
if(nu>200)
{
bill=nu*4.00;
cch=40;
}
else
{
bill=nu*2.00;
cch=40;
}
}
printf("No.of units consumed %d",nu);
printf("\nbill %f",bill);
printf("\nCustomer charges %d",cch);
printf("\nTotal bill %f",bill+cch);
}

Execution1:

Enter the type of connection d/c/t: d

Enter the current month meter reading: 350

Enter the previous month meter reading: 210

No.of units consumed: 140

bill 427.000000

Customer charges: 35

Total bill 462.000000

 

Summery:

  1. Simple if control structure is used to check multiple conditions, out of which any number of conditions may be true.
  2. if-else is used to solve a problem which has only two conditions to check, out of which only a single condition must be true.
  3. else-if ladder is used to check multiple conditions, out of which only a single may be true.

0 comments:

Post a Comment