182k views
5 votes
Energy consumption is measured in units of kilowatt hours (kWh). The more kWh a household use in a month, the higher the energy bill. A power company charges customers $0.12 per kWh for the first 500 kWh. After the first 500 kWh, the rate is $0.15 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. Call the bill_calculator function and pass number of kWh used to it as an argument.

User Edper
by
5.5k points

1 Answer

2 votes

Answer:

The c# program for the scenario is given below.

using System;

public class Program {

double total = 0;

static void Main() {

Program ob = new Program();

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

int p;

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

ob.bill_calculator(p);

Console.WriteLine("Total charge for " + p + " kilo watts of energy is " + ob.total);

}

public void bill_calculator(int e)

{

int first=500;

double chg_one = 0.12, chg_two=0.15;

if(e <= 500)

{

total = (e * chg_one);

}

else if(e > 500)

{

total = ( first * chg_one);

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

}

}

}

OUTPUT

Enter total energy used in kilo watts

5555

Total charge for 5555 kilo watts of energy is 818.25

Step-by-step explanation:

The program is designed to take user input but does not implements validation for the user input.

The program works as follows.

1. Inside main(), the user enters the energy consumed in kilo watts.

2. This is stored in the integer variable, p.

3. The method bill_calculator() is called which accepts the value of p as a parameter.

4. If the energy used is 500 or less kilo watts, charges are different. While for the amount of energy consumption for more than 500, charges are different.

5. The charges for both, 500 and less and more than 500 are stored in double variables.

6. The if-else statements are used to calculate the total charge based on the amount of power used.

if(e <= 500)

{

total = (e * chg_one);

}

else if(e > 500)

{

total = ( first * chg_one);

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

}

7. Since c# is an object-oriented programming language, any method outside main() is called using the object of the class. All code is written inside the class.

8. Next, the total charge is displayed by calling the bill_calculator() method through the object of the Program class.

9. The main() function has only void return type and hence, no value is returned.

User TheAddonDepot
by
5.7k points