131k views
0 votes
The energy company in Program 1 now uses different rates for residential and business customers. Residential customers pay $0.12 per kWh for the first 500 kWh. After the first 500 kWh, the rate is $0.15 per kWh. Business customers pay $0.16 per kWh for the first 800 kWh. After the first 800 kWh, the rate is $0.20 per kWh. Write a program to calculate energy charge. You must write and use the following two functions. (a)A main function: Ask the user to enter number of kWh used and customer type (enter R for residential or B for business). Call the bill_calculator function and pass number of kWh used and customer type to it as arguments. You must use positional arguments to pass kWh used and customer type. (b)A bill_calculator function: This function has two parameters to receive number of kWh used and customer type. Calculate and display the energy charge.

User Nazreen
by
5.1k points

1 Answer

4 votes

Answer:

The c# program for the scenario is given below.

using System;

public class Program2 {

double total = 0;

static void Main() {

Program2 ob = new Program2();

Console.WriteLine("Enter total energy used in kilo watts ");

int p;

p = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter type of customer ");

char t;

t = Convert.ToChar(Console.ReadLine());

ob.bill_calculator(p, t);

Console.WriteLine("Total charge for " + p + " kilo watts electricity for " + t + " customers is " + ob.total);

}

public void bill_calculator(int e, char c)

{

int first=500, second = 800;

double chg_one = 0.12, chg_two=0.15, chg_three = 0.16, chg_four = 0.20;

if(c == 'R')

{

if(e <= 500)

{

total = (e * chg_one);

}

else if(e > 500)

{

total = ( first * chg_one);

total = total + ((e-first) * chg_two);

}

}

if(c == 'B')

{

if(e <= 800)

{

total = (e * chg_three);

}

else if(e > 800)

{

total = ( first * chg_three);

total = total + ((e-second) * chg_four);

}

}

}

}

OUTPUT

Enter total energy used in kilo watts

1111

Enter type of customer

B

Total charge for 1111 kilo watts electricity for B customers is 142.2

Step-by-step explanation:

The program takes user input for the type of customer and the amount of electric current used by that customer.

The two input values decide the rate at which total charge is calculated.

For residential customers, charges are divided into slabs of 500 or less and more than 500.

Alternatively, for business customers, charges are divided into slabs of 800 or less and more than 800.

User input is taken in main function.

Charges are calculated in the bill_calculator method which takes both user input as parameters.

If else block is used to calculate the charges.

No input validation is implemented as it is not mentioned in the question.

All code is written inside the class. An object is created of the class.

The methods outside main() are called using the object of the class.

User StianE
by
5.1k points