Home > OS >  A C program where a discount is applied on the cheaper item
A C program where a discount is applied on the cheaper item

Time:09-05

The program works somewhat but I can't figure out how to make it add the discounted amount to the correct item, basically how the program can tell which item was cheaper apply discount to that item and then add it to the other item that was not discounted (has it's full price since it was more expensive than the other item)

#include "stdio.h"
// a program that takes the price of two items, applies a discount on the cheaper item and then calculates the outcome that needs to be paid.
int main()
{
    float item_one;
    float item_two;
    float discount_percentage= 50;
    float discount_amount;
    float total;
    float cheaper_item;

    printf("\n Insert the price of the first item : ");
    scanf("%f", &item_one);

    printf("\n Insert the price of the first item : ");
    scanf("%f", &item_two);

    if ( item_one < item_two)
        cheaper_item = item_one;
    else
        cheaper_item = item_two;

    discount_amount = (discount_percentage*cheaper_item)/100;

    printf ("\n Discount amount : %f \n", discount_amount);

    total = (discount_amount   cheaper_item);

    printf ("Total to pay : %f \n", total);
}

CodePudding user response:

It is the calculation of the total that is wrong. Add the two prices minus the discount :

total = (item_one   item_two - discount_amount);
  •  Tags:  
  • c
  • Related