Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

total = (item_one + item_two - discount_amount);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading