Calculating the bill for a telephone call is a common requirement in many applications. In this tutorial, we will demonstrate how to write a C program to calculate a customer's total bill for a telephone call, using both a flat rate approach and a slab-based billing system.
Basic Call Billing Program
This first example shows how to bill a call by multiplying the call duration (in minutes) by the rate per minute. Here's the code that performs the basic calculation for a call bill:
Example:
#include <stdio.h>
int main() {
// Declare variables to store the call duration and rate per minute
float callDuration, ratePerMinute, totalBill;
// Prompt the user to enter the call duration in minutes
printf("Enter the duration of the call in minutes: ");
scanf("%f", &callDuration);
// Prompt the user to enter the rate per minute
printf("Enter the rate per minute: ");
scanf("%f", &ratePerMinute);
// Calculate the total bill
totalBill = callDuration * ratePerMinute;
// Display the total bill
printf("The total bill for the call is: %.2f\n", totalBill);
return 0;
}
Output:
Enter the duration of the call in minutes: 10
Enter the rate per minute: 2.5
The total bill for the call is: 25.00
Slab-Based Billing Program
In certain billing systems, varied rates are applied once the number of calls exceeds a specific limit. For instance, the first 100 calls might be charged at a flat rate, while additional calls are charged at progressively lower rates. The program below calculates the telephone bill based on this slab-based model.
Example:
#include <stdio.h>
int main() {
// Declare variables to store the number of calls and the total bill
int callUnits;
float totalBill;
// Prompt the user to enter the number of calls
printf("Enter the number of calls: ");
scanf("%d", &callUnits);
// Calculate the bill based on slabs
if (callUnits <= 100) {
totalBill = 150; // Fixed rate for up to 100 calls
} else if (callUnits > 100 && callUnits <= 200) {
totalBill = 150 + (callUnits - 100) * 0.50; // Rs. 0.50 for each call after 100 up to 200
} else if (callUnits > 200 && callUnits <= 300) {
totalBill = 150 + 100 * 0.50 + (callUnits - 200) * 0.40; // Rs. 0.40 for calls above 200
} else {
totalBill = 150 + 100 * 0.50 + 100 * 0.40 + (callUnits - 300) * 0.30; // Rs. 0.30 for calls above 300
}
// Display the calculated bill
printf("Your total bill is Rs. %.2f\n", totalBill);
return 0;
}
Output:
Enter the number of calls: 250
Your total bill is Rs. 240.00
Explanation:
- User Input: The program takes input for the number of calls.
- Bill Calculation: The bill is calculated based on the number of calls:
- Up to 100 calls are charged at a flat Rs. 150.
- From 101 to 200 calls, an additional Rs. 0.50 is charged for each call.
- From 201 to 300 calls, Rs. 0.40 is charged for each additional call.
- For more than 300 calls, Rs. 0.30 is charged for each additional call.
- Bill Display: The final calculated bill is printed with two decimal places.
Conclusion
In this tutorial, you've learned two methods to calculate telephone call bills in C. Both examples demonstrate handling user input, using conditional logic, and performing arithmetic calculations in C. You can expand on these examples by adjusting the rates or adding more slabs based on your needs.