簡體   English   中英

簡單的C程序從硬幣總數中查找盧比的總量。

[英]Simple C program to find total amount of rupees from total number of coins.

我試圖編寫一個C程序來輸入25派,50派,1盧比和2盧比的硬幣數量,並以盧比為單位計算總金額。 這是我的代碼。 我沒有得到正確的結果。

例如,對於5個25派硬幣的硬幣,我應該得到1.25盧比。 但我得到1.00盧比。 我是C的新手。請指出我的錯誤

#include <stdio.h>

//Q 7d 2011 7th paper Honours 2008 syllabus
int main()

{
int paise25, paise50, rs1, rs2;
double total;
printf("\n Number of coins of:\n\n");
printf(" 25 paise    = ");
scanf("%d",&paise25);
printf(" 50 paise   = ");
scanf("%d",&paise50);
printf(" 1 rupee = ");
scanf("%d",&rs1);
printf(" 2 rupee      = ");
scanf("%d",&rs2);

total=paise25/4 + paise50/2 + rs1 + 2*rs2;
//if(stat<40 || chemistry<40 || physics<40 || math<40 || c<40)

printf("\n Total amount in rupees: Rs %.02f\n",total);

return 0;
}

問題在這一行:

total=paise25/4 + paise50/2 + rs1 + 2*rs2;

當您將一個整數除以另一個整數時,會得到一個整數。 如果要獲取浮點值,則應明確指定此目的。 例如:

total=paise25/4.0 + paise50/2.0 + rs1 + 2*rs2;

請注意我添加的.0部分-它們將有所作為。

當您將2個integers ,小數點將被截斷。

例如,對於整數, 5/4 = 1

從而:

total = ((double)paise25)/4 + ((double)paise50)/2 + rs1 + 2*rs2;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM