Advertisement
smoala

CFunctions

Sep 19th, 2020
1,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. include<stdio.h>
  2. //float price, tax, appleCareCharge, subtotal, total; // used float to store decimals
  3. float computeAppleCareCharges(int y, float p);
  4. float computeAppleSubtotal(float c, float p);
  5. float computeTax(float s);
  6. float computeTotalCharges(float s, float t);
  7.  
  8. int main (void)
  9. {
  10.     //variable declorations
  11.     int years;
  12.     char brand;
  13.     float price = 0.0,
  14.           subtotal = 0.0,
  15.           tax = 0.0,
  16.           total = 0.0;
  17.  
  18.     printf("Enter the price of the phone: ");   //prompt user for price
  19.     scanf("%f", &price);                        // assigns price a value    
  20.     scanf("%c", &brand);                        // remove new line
  21.     printf("Is the phone an iPhone (Y/N)?  ");  //prompts on brand of phone
  22.     scanf("%c", &brand);                        //store case variable "brand"
  23.  
  24.     switch (brand)  //switch to choose answer to brand
  25.     {
  26.         case 'Y':
  27.         case 'y':
  28.             {
  29.             printf("Enter the number of years of AppleCare: ");   // get applecare variable
  30.             scanf("%i", &years);
  31.  
  32.             float charge = computeAppleCareCharges(years, price);
  33.             subtotal = computeAppleSubtotal(charge, price);
  34.             tax = computeTax(subtotal);
  35.             total = computeTotalCharges(subtotal, tax);
  36.             printf("\nPrice of the phone: $%.2f\nAppleCare price: $%.2f\nSubtotal: $%.2f\nTax (5%%): $%.2f\nTotal Price: $%.2f",price, charge, subtotal, tax, total);
  37.  
  38.             break;
  39.             }
  40.  
  41.         case 'N':
  42.         case 'n':
  43.             {
  44.             tax = computeTax(price);
  45.             total = computeTotalCharges(price, tax);
  46.             printf("\nPrice of the phone: $%.2f \nThe tax is: $%.2f \nThe Total is: $%.2f",price,tax,total);
  47.             break;
  48.             }
  49.     }
  50.  
  51.     return (0);     // All C programs need this at the end of main
  52. }
  53.  
  54. float computeAppleCareCharges(int years, float price)
  55. {
  56.     float appleCareCharge = 0.0;
  57.     if (years == 1 )
  58.         appleCareCharge = (price * .12); //code included to compute variables and totals for iPhone w/ 1 year AppleCare, and display results
  59.     else {
  60.         if (years > 1)                              //code included to compute variables and totals for iPhone w/ >1 year AppleCare, and display results
  61.             appleCareCharge = (price * .1) * years;
  62.         else
  63.             printf("You must have at least one year of AppleCare, please start again.");
  64.     }
  65.     return appleCareCharge;
  66. }
  67.  
  68. float computeAppleSubtotal(float appleCareCharge, float price)
  69. {
  70.         float subtotal = appleCareCharge + price;
  71.         return subtotal;
  72. }
  73.  
  74. float computeTax(float subtotal)
  75. {
  76.         return (subtotal * .5);
  77. }
  78.  
  79. float computeTotalCharges(float subtotal, float tax)
  80. {
  81.         return (subtotal + tax);
  82. }
  83.                          
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement