×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

Calculate party expenses using C program

This C program can be used to read item details used in party and calculate all expenses, divide expenses in all friends equally.

This program will read item name, price, quantity and calculate amount (price*quantity), using this program maximum 50 items can be read and calculate the total paid amount. (I think 50 items are enough for a party, it’s a joke actually. You can change the maximum number of items according to party items.

C program to calculate party expenses

#include <stdio.h>
#define MAX 50 //maximum items entry

// structure definition
typedef struct item_details {
    char itemName[30]; // item name
    int quantity; // item quantity
    float price; // per item price
    float totalAmount; // total amount = quantity*price
} item;

int main()
{
    item thing[MAX]; // structure variable
    int i, choice;
    int count = 0;
    float expenses = 0.0f;

    i = 0;
    // infinite loop
    do {
        printf("Enter item details [%2d]:\n", i + 1);

        printf("Item?  ");
        fgets(thing[i].itemName, 30, stdin);

        printf("Price? ");
        scanf("%f", &thing[i].price);

        printf("Quantity?  ");
        scanf("%d", &thing[i].quantity);

        thing[i].totalAmount = (float)thing[i].quantity * thing[i].price;
        expenses += thing[i].totalAmount;

        i++; // increase loop counter
        count++; // increase record counter

        printf("\nWant to more items (press 1): ");
        scanf("%d", &choice);

        getchar();

    } while (choice == 1);

    // print all items
    printf("All details are:\n");
    for (i = 0; i < count; i++) {
        printf("%-30s\t %.2f \t %3d \n %.2f\n", thing[i].itemName, thing[i].price, thing[i].quantity, thing[i].totalAmount);
    }
    printf("#### Total expense: %.2f\n", expenses);

    printf("Want to divide in friends (press 1 for yes): ");
    scanf("%d", &choice);
    if (choice == 1) {
        printf("How many friends? ");
        scanf("%d", &i);
        printf("Each friend will have to pay: %.2f\n", (expenses / (float)i));
    }

    printf("~Thanks for using me... Enjoy your party!!!~\n");
    return 0;
}

Output

Enter item details [ 1]:
Item?  Beer (Kingfisher)
Price? 90.50
Quantity?  10

Want to more items (press 1): 1
Enter item details [ 2]:
Item?  RUM (Old Monk)
Price? 5   
Quantity?  186

Want to more items (press 1): 1
Enter item details [ 3]:
Item?  R.S. Whisky
Price? 250.50
Quantity?  2

Want to more items (press 1): 0
All details are:
Beer (Kingfisher)        90.50    10   905.00
RUM (Old Monk)           186.00    5   930.00
R.S. Whisky             250.50     2   501.00

#### Total expense: 2336.00    
Want to divide in friends (press 1 for yes): 1
How many friends? 6
Each friend will have to pay: 389.33

~Thanks for using me... Enjoy your party!!!~ 

C Structure & Union Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.