用C语言计算用户输入百分比

用C语言计算用户输入百分比,c,percentage,C,Percentage,对于家庭作业,我必须根据C中的用户输入百分比计算折扣百分比,每次运行此程序时,它都会将结果作为原始价格返回,而不是以折扣百分比返回(对于家庭作业,我被告知在不使用百分比运算符的情况下执行此操作) #包括 int main(){ 双价书; 整数百分比; 双倍总额; printf(“这本书的价格是多少?\n”); scanf(“%lf”和价格手册); printf(“折扣前书籍的价格为%.2lf\n\n”,价格为书籍); printf(“将应用多少百分比折扣?\n”); scanf(“%d”、&pe

对于家庭作业,我必须根据C中的用户输入百分比计算折扣百分比,每次运行此程序时,它都会将结果作为原始价格返回,而不是以折扣百分比返回(对于家庭作业,我被告知在不使用百分比运算符的情况下执行此操作)

#包括
int main(){
双价书;
整数百分比;
双倍总额;
printf(“这本书的价格是多少?\n”);
scanf(“%lf”和价格手册);
printf(“折扣前书籍的价格为%.2lf\n\n”,价格为书籍);
printf(“将应用多少百分比折扣?\n”);
scanf(“%d”、&percent);
总计=(100%)/100*价格书;
printf(“\n折扣后的总额为%.2lf\n”、&grand\u总额);
返回0;
}
表达式
(100%)/100
是一个整数表达式,所有涉及的值都是整数,因此您得到整数除法,这将导致值
0

而是使用浮点值:
(100.0%)/100.0

以及修复此问题所需的内容:

printf("\nThe total after discount is %.2lf\n", &grand_total); 
将其更改为:

printf("\nThe total after discount is %.2lf\n", grand_total); 

&
运算符用于获取地址。而且您不可能需要
printf
的地址,就像您需要
scanf
一样。粗略地说,在
scanf()
中,您需要将控制台输入/用户输入放入变量的地址。

我想您可以检查下面的代码,这是有效的,
price\u book
百分比一起计算,请检查下面

#include <stdio.h>

int main(){

    double price_book;
    int percent;
    double grand_total;

    printf("What is the price of the book?\n");
    scanf("%lf", &price_book);

    printf("The Price of the book before discount is %.2lf\n\n",price_book);

    printf("How much percent discount is to be applied?\n");
    scanf("%d", &percent);

    grand_total = price_book - ( (percent)/100.0 * price_book);

    printf("\nThe total after discount is %.2lf\n", grand_total);

    return 0;
}
#包括
int main(){
双价书;
整数百分比;
双倍总额;
printf(“这本书的价格是多少?\n”);
scanf(“%lf”和价格手册);
printf(“折扣前书籍的价格为%.2lf\n\n”,价格为书籍);
printf(“将应用多少百分比折扣?\n”);
scanf(“%d”、&percent);
总计=价格帐簿-((百分比)/100.0*价格帐簿);
printf(“\n折扣后的总额为%.2lf\n”,总计);
返回0;
}

整数除法截断<代码>(100%)/100
始终为0。请在编译时启用警告。您正试图在上一次打印中打印地址
printf
;您只需要
printf(“%.2lf\n”,总计)。尽管如此,
scanf
的参数还是可以的:这里您需要
&
);另外,你所谓的“百分比运算符”实际上被称为“模数”,它的函数与百分比无关。
#include <stdio.h>

int main(){

    double price_book;
    int percent;
    double grand_total;

    printf("What is the price of the book?\n");
    scanf("%lf", &price_book);

    printf("The Price of the book before discount is %.2lf\n\n",price_book);

    printf("How much percent discount is to be applied?\n");
    scanf("%d", &percent);

    grand_total = price_book - ( (percent)/100.0 * price_book);

    printf("\nThe total after discount is %.2lf\n", grand_total);

    return 0;
}