This blog is under construction

Saturday 22 June 2013

C program to calculate electricity bill

Write a C program to generate electricity bill.



  #include <stdio.h>
  int main() {
        int units;
        float rate;
        printf("Enter the no of units consumed:");
        scanf("%d", &units);
        if (units <= 100) {
                /* less 31 units of consumption - 30 rupees */
                if (units <= 30) {
                        rate = 30;
                } else {
                       /* greater than 30 & less than 101 - 1Rs/unit */
                        rate = 1 * units;
                }
        } else if (units >= 101 && units <=200) {
                rate = 1.5 * units;
        } else if (units >= 201 && units <= 500) {
                rate = 3 * units;
        } else {
                rate = 5 * units;
        }
        printf("Bill amount: %.3f\n", rate);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the no of units consumed:155
  Bill amount: 232.500



No comments:

Post a Comment