Tariff Calculation Program in C

A tariff program is a useful example of range-based if...else logic. This version calculates the charge by slabs so each unit range is priced separately instead of applying one rate to the entire consumption.

Example Tariff Slabs Top ↑

  • First 100 units: 1.50 per unit
  • Next 100 units: 2.00 per unit
  • Units above 200: 3.00 per unit

Progressive Slab Calculation Top ↑

#include <stdio.h>

int main(void) {
    double units = 250.0;
    double charge;

    if (units < 0.0) {
        printf("Units cannot be negative.\n");
        return 1;
    }

    if (units <= 100.0) {
        charge = units * 1.50;
    } else if (units <= 200.0) {
        charge = 100.0 * 1.50
               + (units - 100.0) * 2.00;
    } else {
        charge = 100.0 * 1.50
               + 100.0 * 2.00
               + (units - 200.0) * 3.00;
    }

    printf("Charge = %.2f\n", charge);
    return 0;
}

Tariff from User Input Top ↑

#include <stdio.h>

int main(void) {
    double units;

    printf("Enter units consumed: ");
    if (scanf("%lf", &units) != 1 || units < 0.0) {
        printf("Enter a valid non-negative number.\n");
        return 1;
    }

    double charge;

    if (units <= 100.0) {
        charge = units * 1.50;
    } else if (units <= 200.0) {
        charge = 150.0
               + (units - 100.0) * 2.00;
    } else {
        charge = 350.0
               + (units - 200.0) * 3.00;
    }

    printf("Total charge = %.2f\n", charge);
    return 0;
}
The rates above are tutorial values, not a real utility tariff. Replace them with the rules required by your own exercise.




plus2net.com



17-01-2021


REM It's an exam program Print “ ” A= 50 B= 30 C = A * B Print “ The result is “, C



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer