何かの支払いに必要なコインの量を計算するプログラムを作る必要があります。 I.C数学の割り当て
total cost = $1.35
$1.00 = 1, remaining $0.35
$0.25 = 1, remaining $0.10
$0.10 = 1, remaining $0.0
$0.05 = 0, remaining $0.0
$0.01 = 0, remaining $0.0`
私の質問はどのようにモジュラスを使ってこれらの演算を行うのですか?割り当ての一環として、モジュラスと整数除算を使用して計算を行う必要がありますが、このコードでモジュラスを使用する方法はわかりません。私が始めたコードは次のとおりです。
#include<stdio.h>
#include<math.h>
int main(){
float topay, change;
float gst = 1.13; //gst amount
int loonies, quatres, nickles, dimes, pennies; //coin variables
printf ("Please enter the amount to be paid: $");
scanf ("%f", &topay); //Input for amount to be paid
printf ("GST: %.2f\n", gst);
printf ("Balance owing: $%.2f\n", topay=topay*gst);
loonies = topay; //Math for amount of loonies
change = topay - loonies; //Math for balance owing
quatres = change/0.25; //Math for how many quarters needed
printf ("Loonies required: %d, balance owing $%.2f\n", loonies, change);
printf ("Quarters required: %d, balance owing $%.2f\n", quatres, change = change-(quatres*0.25));
return 0;
}
ドルを表す浮動小数点数ではなく、セントの数を表すintを使用します。 – jsheeran