使用模数/ fmod和if语句c ++

时间:2015-10-22 00:17:16

标签: c++

问题:你有1.45美元我们怎样才能输出这个是多少分之三和一角钱

产量应为5季度2角钱1镍。

double amount = 1.45
double quarter = .25
double dime = .10
double nickel = .05
if(fmod (amount,quarter) == 0)
cout << "your change is"<< amount/quarter << " quarters" ;

1 个答案:

答案 0 :(得分:1)

转换为美分(使用int算术得分,因为它是离散值)为1.45 * 100然后

int quarters = 145 / 25;
int dimes = ( 145 - quarters * 25 ) / 10;
int nickels = ( 145 - quarters * 25 - dimes * 10 ) / 5;