The Program in C that simulates working of telephone bill company is given below:
#include <stdio.h>
#define LOCAL_CALL_RATE 0.5
#define LONG_DISTANCE_CALL_RATE 1.5
#define INTERNATIONAL_CALL_RATE 5.0
struct customer {
char name[50];
int local_call_duration;
int long_distance_call_duration;
int international_call_duration;
};
double calculate_bill(struct customer* c) {
double local_call_charge, long_distance_call_charge, international_call_charge, total_bill;
local_call_charge = c->local_call_duration * LOCAL_CALL_RATE;
long_distance_call_charge = c->long_distance_call_duration * LONG_DISTANCE_CALL_RATE;
international_call_charge = c->international_call_duration * INTERNATIONAL_CALL_RATE;
total_bill = local_call_charge + long_distance_call_charge + international_call_charge;
return total_bill;
}
void print_bill(struct customer* c, double bill) {
printf("Telephone Bill for %s\n", c->name);
printf("Local Call Duration: %d minutes\n", c->local_call_duration);
printf("Long Distance Call Duration: %d minutes\n", c->long_distance_call_duration);
printf("International Call Duration: %d minutes\n", c->international_call_duration);
printf("Total: $%.2lf\n", bill);
}
int main() {
struct customer c;
double bill;
printf("Enter customer name: ");
scanf("%s", c.name);
printf("Enter Local call duration (minutes): ");
scanf("%d", &c.local_call_duration);
printf("Enter Long Distance call duration (minutes): ");
scanf("%d", &c.long_distance_call_duration);
printf("Enter International call duration (minutes): ");
scanf("%d", &c.international_call_duration);
bill = calculate_bill(&c);
print_bill(&c, bill);
return 0;
}
Output:
Enter customer name: KUMAR
Enter Local call duration (minutes): 10
Enter Long Distance call duration (minutes): 30
Enter International call duration (minutes): 30
Telephone Bill for KUMAR
Local Call Duration: 10 minutes
Long Distance Call Duration: 30 minutes
International Call Duration: 30 minutes
Total: $200.00
Pro-Tips💡
This program uses a struct to represent a customer, and the struct contains the customer’s name and the duration of their local, long-distance, and international calls.
The main()
function prompts the user to enter the customer’s information, which is read using scanf()
.
The calculate_bill()
function is then called and passed a pointer to the customer struct.
It calculates the total bill by multiplying the call durations by the appropriate rates and adding up the charges.
The print_bill()
function is then called and passed a pointer to the customer struct and the bill, it then prints the customer’s name, call durations, charges and total bill using printf()
.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.