In: Computer Science
In the space provided below write a C program that computes the total cost of items you want to order online. It does so by first asking how many items are in your shopping cart and then based on that number, it repeatedly asks you to enter the cost of each item. It then adds a 5% delivery charge for standard delivery if the total is below $1,000, and an additional 10% charge if you want an expedited next day delivery.
Code
#include<stdio.h>
int main()
{
//variable declaration
int n,i;
float shipping_cost=0, total_cost=0;
char ch;
//take input number of items
printf("Enter how many items are there in shopping cart: ");
scanf("%d", &n);
//array to store cost of items
int cost[n];
//take cost of items as input
printf("Enter the items cost\n");
for(i=0;i<n;i++)
{
printf("Enter the cost of item %d: ",i+1);
scanf("%d",&cost[i]);
}
//add the elements in the cost array
for(i=0;i<n;i++)
{
total_cost = total_cost + cost[i];
}
// 5 % shipping charge if total cost is less than 1000
if(total_cost < 1000)
shipping_cost = 0.05 * total_cost;
// check whether the user wants next day delivery
printf("Do you want next day delivery: (y or n)\n");
scanf(" %c",&ch);
// add 10% extra shipping charge if the user wants next day delivery
if(ch == 'y')
shipping_cost = shipping_cost + (total_cost * 0.10);
// calculate the total cost
total_cost = total_cost + shipping_cost;
//print total cost
printf("Total Bill Amount: %f\n", total_cost);
}
Output Screenshot
Comments has been provided in the code and also output screenshot has been given for the reference.
Please execute the code in C IDE to see the ouput.