Skip to content

Instantly share code, notes, and snippets.

@dbc2201
Created November 25, 2018 12:18
Show Gist options
  • Save dbc2201/7685e8df0d3eb8265af849c7513e62db to your computer and use it in GitHub Desktop.
Save dbc2201/7685e8df0d3eb8265af849c7513e62db to your computer and use it in GitHub Desktop.
Sample Answer for Worksheet 6 Question 4
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
#define METER_CHARGE 50
double make_bill();
int main()
{
make_bill();
return 0;
}
double make_bill()
{
char name[SIZE];
double units = 0.0;
double billed_amount = 0.0;
printf("Hello, please enter your name: ");
scanf("%[^\n]s", name);
printf("Welcome, please enter the units you have consumed: ", name);
scanf("%lf", &units);
if ( units < 100.0 )
{
billed_amount = 40.0 * units;
}
else if ( units > 100.0 && units < 200.0 )
{
billed_amount = 50.0 * units;
}
else if ( units > 300.0 )
{
billed_amount = 60.0 * units;
}
billed_amount += METER_CHARGE;
printf("\nName : %s\n", name);
printf("Billed Amount : Rs. %.2lf\n", (billed_amount / 100));
return billed_amount;
}
@dbc2201
Copy link
Author

dbc2201 commented Nov 25, 2018

Note you can also call the function make_bill() inside a loop to enter multiple values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment