Wednesday, September 16, 2015

Electricity Charges (CPROM Uitm Arau 2015)

Rangkaian Tenaga charges electricity usage based on the following on the following table:

Usage(kWh per Month) Rates per kWh
For the first 250 kWh 0.10
For the next 200 kWh 0.20
For the next 200 kWh 0.30
For the next kWh above 650 kWh 0.50


The minimum monthly charge is RM 5.00 As a programmer, you are required to develop a system that calculate the electricity charges based on the above table.

Input : User is required to input the usage of electricity in kWh.
Output :The program will display the electricity charges.

InputOutput
26027
700150

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Solved By Afiq
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;

public class Q2{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      
      String line =System.getProperty("line.separator");
      scan.useDelimiter(line);
            
      int use = scan.nextInt();
      
      double total = 0;
      
      if(use<=250){
         total = use*0.10;
      }
      else if(use>250){
         total = 250*0.10;
         use = use - 250;
         
         if(use<=200){
            total = total + (use*0.2);
         }
         else if(use>200){
            total = total + (200*0.2);     
            use = use - 200;       
            if(use<=200){
               total = total + (use*0.3);   
            }
            else if(use > 200){
               total = total + (200*0.3);
               use = use - 200;
               total = total + (use*0.5);
            }   
         }
      }
      
      System.out.print(total);
   }
}


Judge Answers in C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <iostream>
using namespace std;
int main() {
    float kWh,charges;
    cin>>kWh;
    if(kWh<=50 ) charges=5.00;
    else if(kWh>50 && kWh<=250) charges=kWh*0.10;
    else if(kWh>250 && kWh<=450) charges=25+((kWh-250)*0.20);
    else if(kWh>450 && kWh<=650) charges=65+((kWh-450)*0.30);
    else charges=125+((kWh-650)*0.50);
    cout<<charges;
}

No comments:

Post a Comment