lesson 6 mathvars c
[[lesson_6_mathvars_c]] last edit on
Aug 9, 2005
2:14 AM
by rescued_by_c
[user02@FC4 Lesson06]$ cat -n mathvars.c
[user02@FC4 Lesson06]$ ./mathvars
Item Cost: $15.500000 Tax: $0.930000 Total: $16.430000
Customer change: $3.570000
[user02@FC4 Lesson06]$ cat -n mathvars2.c
[user02@FC4 Lesson06]$ ./mathvars2
Item Cost: $15.50 Tax: $0.93 Total: $16.43
Customer change: $3.57
1 #include <stdio.h>
2
3 int main(void)
4 {
5 float cost = 15.50; // The cost of an item
6 float sales_tax = 0.06; // Sales tax is 6 percent
7 float amount_paid = 20.00; // The amount the buyer paid
8 float tax, change, total; // Sales tax, buyer change and
9 // total bill
10
11 tax = cost * sales_tax;
12 total = cost + tax;
13 change = amount_paid - total;
14
15 printf("Item Cost: $%f\tTax: $%f\tTotal: $%f\n", cost, tax, total);
16 printf("Customer change: $%f\n", change);
17
18 return 0;
19 }
[user02@FC4 Lesson06]$ gcc -o mathvars mathvars.c[user02@FC4 Lesson06]$ ./mathvars
Item Cost: $15.500000 Tax: $0.930000 Total: $16.430000
Customer change: $3.570000
[user02@FC4 Lesson06]$ cat -n mathvars2.c
1 #include <stdio.h>
2
3 int main(void)
4 {
5 float cost = 15.50; // The cost of an item
6 float sales_tax = 0.06; // Sales tax is 6 percent
7 float amount_paid = 20.00; // The amount the buyer paid
8 float tax, change, total; // Sales tax, buyer change and
9 // total bill
10
11 tax = cost * sales_tax;
12 total = cost + tax;
13 change = amount_paid - total;
14
15 printf("Item Cost: $%.2f\tTax: $%.2f\tTotal: $%.2f\n", cost, tax, total);
16 printf("Customer change: $%.2f\n", change);
17
18 return 0;
19 }
[user02@FC4 Lesson06]$ gcc -o mathvars2 mathvars2.c[user02@FC4 Lesson06]$ ./mathvars2
Item Cost: $15.50 Tax: $0.93 Total: $16.43
Customer change: $3.57