简体   繁体   中英

Calculate the discount in a program in C language

I have a plan that gives the total price of the products and if the purchase is more than 200, it should give a 15% discount. But when displaying the final amount, it displays the zero:

#include <stdio.h>
#include <conio.h>

int main()
{
    int count;
    printf("plz enter number of product :");
    scanf("%d", &count);
    int price;
    int allprice;
    float discounted_price ;
    int i = 0;
    while(i<count)  
    {
        printf("plz enter price  %d : ",i+1);
        scanf("%d", &price);
        allprice +=price;
        i++;            
    }
    if(allprice>200)
    {
    float discount_amount = (15*allprice)/100;
    float discounted_price = (allprice-discount_amount);    
    } 

    printf("price before discount : %d ",allprice);
    printf("\n");
    printf("price after discount : %d ",discounted_price);
    return 0;
}

You have discounted_price twice.

Once where you calculate it inside the if .
Once outside, which you output.
Outputting hence ignores the calculated value.

Change

float discounted_price = (allprice-discount_amount);

to

discounted_price = (allprice-discount_amount);

And you also need to change the way of printing it, to match the float type
(and thereby avoid undefined behaviour).

printf("price after discount : %f ",discounted_price);

Finally, the amounts will be more precise if you avoid the integer division:

float discount_amount = (15*allprice)/100.0;

And for good measure, init the summation variable (though the effect of that is not always seen):

int allprice =0;

For readining input by a human (ie prone to format errors) it would be wise to check the return value of scanf() and use other verification techniques. But that is beyond the scope of an answer to your question.

First, you should initialize allprice to zero in order to calculate the total. The inital value of the variable, if not initialized is undefined.

The expression

(15*allprice)/100; 

may result in zero because it's doing integer divion since all of the operands (15, allprice, 100) are integers. To avoid this, you can just convert one of the operands to a float , or just add a.0 after 100.

 (15*allprice)/100.0f; 

This should fix your problem. Let me know if it helps.

The resulting code should look like this:

#include <stdio.h>
#include<conio.h>

int main(){
int count;
printf("plz enter number of product :");
scanf("%d", &count);
int price;
int allprice = 0;
float discounted_price ;
int i = 0;
while(i<count)  
{
    printf("plz enter price  %d : ",i+1);
    scanf("%d", &price);
    allprice +=price;
    i++;            
}
if(allprice>200)
{
float discount_amount = (15*allprice)/100.0f;
discounted_price = (allprice-discount_amount);    
} 

printf("price before discount : %d ",allprice);
printf("\n");
printf("price after discount : %f ",discounted_price);
return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM