0% found this document useful (0 votes)
11 views5 pages

C Program for Electricity Charges Calculation

The document outlines two C programs: one for calculating electricity charges based on usage with a tiered pricing structure and a minimum charge, and another for computing the sine of an angle using Taylor series approximation. The electricity program prompts for user input, calculates charges, and applies a surcharge if necessary, while the sine program compares results from a custom implementation and a built-in library function. Both programs include algorithms, code snippets, and test cases to validate their functionality.

Uploaded by

gowdassamrudh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

C Program for Electricity Charges Calculation

The document outlines two C programs: one for calculating electricity charges based on usage with a tiered pricing structure and a minimum charge, and another for computing the sine of an angle using Taylor series approximation. The electricity program prompts for user input, calculates charges, and applies a surcharge if necessary, while the sine program compares results from a custom implementation and a built-in library function. Both programs include algorithms, code snippets, and test cases to validate their functionality.

Uploaded by

gowdassamrudh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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@[Link]
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@[Link]: [Link] Page 2


CAMBRIDGE INSTITUTE OF TECHNOLOGY
K.R. PURAM, BANGALORE – 560 036, Ph: 080-2561 8798 / 2561 8799
Fax: 080-2561 8789, email: principal@[Link]
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@[Link]: [Link] Page 3


CAMBRIDGE INSTITUTE OF TECHNOLOGY
K.R. PURAM, BANGALORE – 560 036, Ph: 080-2561 8798 / 2561 8799
Fax: 080-2561 8789, email: principal@[Link]
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”,&degree);
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@[Link]: [Link] Page 4


CAMBRIDGE INSTITUTE OF TECHNOLOGY
K.R. PURAM, BANGALORE – 560 036, Ph: 080-2561 8798 / 2561 8799
Fax: 080-2561 8789, email: principal@[Link]
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@[Link]: [Link] Page 5

Common questions

Powered by AI

A tiered structure promotes conservation by increasingly penalizing higher consumption through higher rates, encouraging users to limit usage to lower-cost brackets. In contrast, a flat-rate system charges the same per unit, irrespective of quantity, offering no such incentives. Tiered structures can effectively nudge behavioral change towards sustainable energy use while providing economic signals aligned with policy goals .

A 15% surcharge on bills exceeding Rs 400 disproportionately affects high-consumption users, potentially reviewing more from higher socio-economic brackets. It could incentivize energy conservation but might also penalize larger households or those with legitimate higher energy needs, raising questions of fairness. Balancing motivational and punitive aspects is critical for equitable utility billing policy .

The program ensures accuracy in the Taylor series approximation of Sin(x) by using a do-while loop that continues until the term being added to the summation is smaller than 0.000001 in magnitude. This small value sets a precision threshold, ensuring that the series converges close to the actual value of the sine function .

To compute Sin(x) using the Taylor series, the program first converts the angle from degrees to radians. It initializes variables for the numerator, denominator, and iteration counter. A do-while loop calculates terms of the Taylor series until a term is smaller than a threshold. Each term is added to a cumulative sum representing the approximate sine. After computing the approximation, the C program uses a library function to calculate sine for comparison, printing both results for evaluation .

Including a minimum meter charge ensures that the utility provider covers basic infrastructure and administrative costs irrespective of usage levels. It justifies the economic model for utility maintenance and ensures consistent revenue streams, addressing the risk of revenue shortfalls when users consume minimal electricity .

Using user-defined functions in C allows for modular and organized code, enhancing readability and maintainability. For complex computations like Sin(x) with the Taylor series, it enables separation of logic for the series computation, making it easier to test, debug, and reuse parts of the program without interfering with the main logic flow .

Test cases validate the functionality by comparing expected and actual outputs for given inputs. For instance, entering 200 units should yield Rs 260, and entering 300 units should yield Rs 350. With 400 units, the output should be Rs 517.5, confirming correct application of tiered pricing and surcharges. Successful matching of expected and obtained outputs indicates the program's correctness .

The C program calculates electricity charges using a tiered pricing structure. For the first 200 units, it charges 80 paise per unit. For 201 to 300 units, it charges 90 paise per unit. Beyond 300 units, Rs 1 per unit is charged. A minimum meter charge of Rs 100 is applied to all users. Additionally, if the total amount exceeds Rs 400, a 15% surcharge is added to the final amount .

The "else-if ladder" is crucial for correctly applying the tiered pricing structure based on units consumed. Each condition evaluates the range of units, applying relevant pricing rules. This structure ensures that only necessary calculations are performed, optimizing computational efficiency by avoiding unnecessary checks after a condition is satisfied .

The initialization of PI as 3.142 impacts the accuracy of results since it approximates the mathematical constant π. This affects calculations for converting degrees to radians. A more precise representation of π could yield more accurate sine results, reflecting the importance of precision for mathematical constants in programming .

You might also like