0% found this document useful (0 votes)
15 views6 pages

2023gp15012 SandipanRoy

The document contains C source code for a program that calculates discounts on shopping cart totals. It defines functions to calculate discounts based on cart value or product category. It then prompts the user to input item details, calculates subtotals, applies discounts and prints the final grand total.

Uploaded by

SOUMYADIP MAITY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

2023gp15012 SandipanRoy

The document contains C source code for a program that calculates discounts on shopping cart totals. It defines functions to calculate discounts based on cart value or product category. It then prompts the user to input item details, calculates subtotals, applies discounts and prints the final grand total.

Uploaded by

SOUMYADIP MAITY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name : Sandipan Roy.

BITS ID : 2023gp15012.
Source Code:

#include <stdio.h>

// Function to calculate discount based on cart value


float calculateCartDiscount(float totalCartValue) {
if (totalCartValue >= 50000 && totalCartValue < 100000) {
return 0.05; // 5% discount
} else if (totalCartValue >= 100000 && totalCartValue < 200000) {
return 0.1; // 10% discount
} else if (totalCartValue >= 200000 && totalCartValue < 300000) {
return 0.15; // 15% discount
} else if (totalCartValue >= 300000) {
return 0.2; // 20% discount
} else {
return 0; // No discount
}
}

// Function to calculate discount based on product category


float calculateCategoryDiscount(char category[]) {
if (strcmp(category, "Electronics") == 0) {
return 0.07; // 7% discount for Electronics
} else if (strcmp(category, "Clothing") == 0) {
return 0.12; // 12% discount for Clothing
} else if (strcmp(category, "Groceries") == 0) {
return 0.05; // 5% discount for Groceries
} else {
return 0; // No discount for other categories
}
}

int main() {
// Arrays to store item details
char items[100][50];
int quantities[100];
float prices[100];
int i;
int numItems;
float totalCartValue = 0;

// Input number of items


printf("Enter the number of items: ");
scanf("%d", &numItems);

// Input item details


for (i = 0; i < numItems; i++) {
printf("Enter details for item %d:\n", i + 1);
printf("Name: ");
scanf("%s", items[i]);
printf("Quantity: ");
scanf("%d", &quantities[i]);
printf("Price per item: ");
scanf("%f", &prices[i]);
}

printf("\nItem\t\t\tQuantity\t\tPrice\t\tSub-Total\n");
printf("---------------------------------------------------------------------------------\n");
for (i = 0; i < numItems; i++) {
printf("%s\t\t\t%d\t\t\t%.2f\t\t%.2f\n", items[i], quantities[i], prices[i],
quantities[i] * prices[i]);
}
float subtotals[numItems];
float total = 0;
for (i = 0; i < 5; i++) {
subtotals[i] = quantities[i] * prices[i];
total += subtotals[i];
}

// Calculate and display total cart value


printf("\nTotal %-10.2f\n", total);

// Calculate and display cart discount


float cartDiscount = totalCartValue * calculateCartDiscount(totalCartValue);
printf("Cart Value Discount %.0f%% -%.2f\n",
calculateCartDiscount(totalCartValue) * 100, cartDiscount);

// Calculate and display category-wise discounts


float electronicsDiscount = 0, clothingDiscount = 0, groceriesDiscount = 0;
for (i = 0; i < numItems; i++) {
if (strcmp(items[i], "Laptop") == 0 || strcmp(items[i], "Smartphone") == 0)
{ // comparing the string if the word is same or not.
electronicsDiscount += quantities[i] * prices[i] *
calculateCategoryDiscount("Electronics");
} else if (strcmp(items[i], "Shirt") == 0 || strcmp(items[i], "Jeans") == 0) {
clothingDiscount += quantities[i] * prices[i] *
calculateCategoryDiscount("Clothing");
} else if (strcmp(items[i], "Milk") == 0) {
groceriesDiscount += quantities[i] * prices[i] *
calculateCategoryDiscount("Groceries");
}
}

printf("Electronics Discount %.0f%% -%.2f\n",


calculateCategoryDiscount("Electronics") * 100, electronicsDiscount);
printf("Clothing Discount %.0f%% -%.2f\n",
calculateCategoryDiscount("Clothing") * 100, clothingDiscount);
printf("Groceries Discount %.0f%% -%.2f\n",
calculateCategoryDiscount("Groceries") * 100, groceriesDiscount);

// Calculate and display grand total


float grandTotal = total - (cartDiscount + electronicsDiscount +
clothingDiscount + groceriesDiscount);
printf("\nGrand Total %.2f\n", grandTotal);

return 0;
}
Output Screen :

You might also like