Program 3:
Title: Design and Develop a C program for an electricity board to calculate charges
based on electricity usage. The program should prompt the user to input their name
and the number of units consumed.
Problem Description: Using a tiered pricing structure, calculate the charges as
follows:
• For the first 200 units, charge 80 paise per unit
• For the next 100 units (201 to 300), charge 90 paise per unit.
• Beyond 300 units, charge Rs 1 per unit.
Additionally, apply a minimum meter charge of Rs. 100 to all users. If the total
amount exceeds Rs 400, include a 15% surcharge. Display the user's name and the
total charges incurred, accounting for the surcharge if applicable.
Method: else-if ladder.
Algorithm:
Step 1:[Initialize]
Start
Step 2:[Read the name of the user and number of units of electricity
consumed]
Read name,units
Step 3: [Use else if ladder, to satisfy the given condition: for the first 200 units 80
paise per unit: for the next 100 units 90 paise per unit: beyond 300
units Rs 1 per unit. All users are charged a minimum of Rs.100 as meter
charge]
if(units<=200)
rupees=units*0.80
rupees=rupees+100
else if(units<=300 && units>200)
rupees=200*0.80+(units-200)*0.90
rupees=rupees+100
else
rupees=200*0.80+100*0.90+(units-300)*1.00
rupees=rupees+100
end if
CAMBRIDGE INSTITUTE OF TECHNOLOGY
K.R. PURAM, BANGALORE – 560 036, Ph: 080-2561 8798 / 2561 8799
Fax: 080-2561 8789, email: principal@cambridge.edu.in
Affiliated to VTU|Approved by AICTE|NAAC & NBA Accredited|An ISO9001:2015 Certified Institute
Department: Computer Science & Engineering
Step 4:[Use simple if to satisfy, the total amount is more than Rs 400, then an
additional surcharge of 15% of total amount is charged]
if(rupees>400)
rupees=rupees+0.15*rupees
end if
Step 5:[Print the name of user and amount to be paid]
Print name, rupees
Step 6:[Finished]
Program:
#include<stdio.h>
int main( )
{
char name[20];
int units;
float rupees=0;
printf("\n enter the name of the user :");
gets(name);
printf("\n enter number of units consumed :");
scanf("%d",&units);
if(units<=200)
rupees=units*0.80+100;
else if(units<=300 && units>200)
rupees=200*0.80+(units-200)*0.90+100;
else
rupees=200*0.80+100*0.90+(units-300)*1.00+100;
if(rupees>400)
rupees=rupees+0.15*rupees;
printf("%s has to pay rupees %f",name,rupees);
}
Test Cases:
Test No Input Parameters Expected Output Obtained Output
Enter the name of the user: Raj Raj has to pay Raj has to pay
1
Enter number of units consumed: 200 rupees: 260 rupees: 260
2 Enter the name of the user: Anil Anil has to pay Anil has to pay
Enter number of units consumed: 300 rupees: 350 rupees: 350
email: iqac@cambridge.edu.inWebsite: www.cambridge.edu.in Page 2
CAMBRIDGE INSTITUTE OF TECHNOLOGY
K.R. PURAM, BANGALORE – 560 036, Ph: 080-2561 8798 / 2561 8799
Fax: 080-2561 8789, email: principal@cambridge.edu.in
Affiliated to VTU|Approved by AICTE|NAAC & NBA Accredited|An ISO9001:2015 Certified Institute
Department: Computer Science & Engineering
Enter the name of the user: Kiran Kiran has to pay Kiran has to pay
3
Enter number of units consumed: 400 rupees: 517.5 Rupees: 517.5
Program 4:
Title: Design and Develop a C Program using functions to compute Sin(x) using Taylor series
approximation. Compare your result with the built- in Library function. Print both the results
with appropriate messages.
Problem Description: The program should prompt the user to input the angle x, then calculate
its sine value using the Taylor series expansion.
Method: do-while loop, user-defined functions.
Algorithm: (Sin x)
Step 1:[Initialization]
Start
Step 2: [Set PI=3.142]
PI=3.142
Step 3: [Reading degree]
read degree
Step 4:[Calculate x to convert degree to radians]
x = degree*(PI/180)
Step 5:[Set the intial values of nume,deno,i]
nume=x
deno=1
i=2
Step 6:[Iterate using do while loop till term>=0.000001]
do
term=nume/deno
nume=-nume*x*x
deno=deno*i*(i+1)
sum=sum+term
i=i+2
while(fabs(term)>=0.000001);
Step 7: [Display the result]
Print degree and sum.
email: iqac@cambridge.edu.inWebsite: www.cambridge.edu.in Page 3
CAMBRIDGE INSTITUTE OF TECHNOLOGY
K.R. PURAM, BANGALORE – 560 036, Ph: 080-2561 8798 / 2561 8799
Fax: 080-2561 8789, email: principal@cambridge.edu.in
Affiliated to VTU|Approved by AICTE|NAAC & NBA Accredited|An ISO9001:2015 Certified Institute
Department: Computer Science & Engineering
Also, print using built in function sin(x).
Step 8: [Finished]
Stop
Program:
#include<stdio.h>
#include<math.h>
#define PI 3.142
int main( )
{
int i,degree;
float x,sum=0,term,nume,deno;
printf(“enter the value of degree”);
scanf(“%d”,°ree);
x=degree * (PI/180);
nume=x;
deno=1;
i=2;
do
{
term=nume/deno;
nume=-nume*x*x;
deno=deno*i*(i+1);
sum=sum+term; i=i+2;
}while(fabs(term)>=0.00001);
printf(“\nThe sine of %d is %f”,degree, sum);
printf(“\nThe sine function of %d is %f using library function”, degree, sin(x));
}
email: iqac@cambridge.edu.inWebsite: www.cambridge.edu.in Page 4
CAMBRIDGE INSTITUTE OF TECHNOLOGY
K.R. PURAM, BANGALORE – 560 036, Ph: 080-2561 8798 / 2561 8799
Fax: 080-2561 8789, email: principal@cambridge.edu.in
Affiliated to VTU|Approved by AICTE|NAAC & NBA Accredited|An ISO9001:2015 Certified Institute
Department: Computer Science & Engineering
Test Cases:
Test No Input Parameters Expected Output Obtained Output
The sin(0)is 0 The sin(0)is 0
1 Degree = 0 The sine function 0 is 0 The sine function 0 is 0
using library function using library function
The sin(60) is 0.866093 The sin(60) is 0.866093
2 Degree = 60 The sine function 60 is The sine function 60 is
0.866093 using library 0.866093 using library
function function
email: iqac@cambridge.edu.inWebsite: www.cambridge.edu.in Page 5