c++ - Calculate Change Without If/While/For Statement or any data structures -


i trying write program generates minimum amount of change (quarters, nickels, dimes, pennies), stipulation can't use kind of if, for, while statements.

i'm sure possible, i'm extremely limited, , can't seem figure out how make work without breaking rules.

the following code works, accept when runs value of 0, , breaks while trying use modulus on 0 ( think):

#include <iostream>  int main() {      int cents;      std::cout << "enter amount in cents less dollar:" << std::endl;     std::cin >> cents;      int quarters = cents/25;     int remainder = cents % (quarters*25);     int dimes = remainder/10;     remainder = remainder % (dimes*10);     int nickels = remainder/5;     remainder = remainder % (nickels*5);     int pennies = remainder/1;      std::cout << "your change be: " << "\n q: " << quarters << "\n d: " << dimes << "\n n: " << nickels << "\n p: " << pennies;      return 0; } 

in line:

    remainder = remainder % (dimes*10); 

you allow remainder become 0. cause division 0 here:

    int nickels = remainder/5; //0     remainder = remainder % (nickels*5); // modulo 0 % 0 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -