English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

If I wanted to create a program that when I enter in a number from 1-99 it will break the number down for me in the least amount of coins... Say I put in 83, it should say 3 quaters 1 nickel and 3 pennies.
You can email me if you'd like!!

2007-01-14 13:53:25 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

//MS Visual C++ - tested => result is working fine.
#include "stdafx.h"
#include

int _tmain(int argc, _TCHAR* argv[])
{

int am; //the initial amount
int quarters; //how many quarters $0.25
int dimes; //how many dimes as $0.10
int nickels; // how many nickels as $0.05
int pennies; //how many pennies as $0.001


std::cout << "\n Amount ( 1 - 99) : \t";
std::cin >> am; //read the amount which need to be splitted

if (am >= 1 && am <= 99) //check if the amount is between 1 - 99
{ //if ok continue
quarters = am / 25; //extracting quarters as integer
am = am % 25; //remainder - am mod 25 will be the new amount

dimes = am / 10; //extracting dimes
am = am % 10; //remainder - am mod10 will be the new amount

nickels = am /5; //extracting nickels
pennies = am % 5; // remainder - am mod5 are the pennies
//printing results only if NOT zero
if(quarters)
std::cout << "Quarters: \t" << quarters <<'\n';
if (dimes)
std::cout << "Dimes:\t\t" << dimes << '\n';
if(nickels)
std::cout << "Nickles: \t" << nickels << '\n';
if(pennies)
std::cout << "Pennies: \t" << pennies << '\n';
}
else //print error message if the amount it is NOT 1-99
std::cout << "\n Error! \a \a \a Amount should be 1 - 99. Have a nice day ! \n";

return 0;
}

2007-01-14 15:03:30 · answer #1 · answered by dand370 3 · 0 0

The best way would be to do a mod25, then subtract from the original, then do a mod10, subtract, mod5, subtract etc until there's nothing left. The values would tell you how many of each coin is needed to make the total.

Don't forget to convert dollars to cents first. Enjoy.

2007-01-14 14:03:11 · answer #2 · answered by Anonymous 7 · 1 0

Pretty straight forward...i am too lazy to clean up the code or check it so you will have to clean it up but its right:

char a[10];
gets(a);
int x = itoa(a);
int z;
int quarters=0;dimes=0,nickles=0, pennies=0;

quarters = x/25;
z = x%25;
dimes = z/10;
z = z%10;
nickles = z/5;
pennies = z%5;

printf("Quarters = %d Dimes = %d Nickles = %d Pennies = %d",quarters,dimes,nickles,pennies);

2007-01-14 14:08:39 · answer #3 · answered by Anonymous · 0 0

void func( int x, int& qtrs, int & dimes, int& nickles, int& cents)
{
qtrs = x / 25;
x = x % 25;
dimes = x / 10;
x = x % 10;
nickels = x / 5;
cents = x % 5;
}

2007-01-14 14:03:13 · answer #4 · answered by arun_uj 1 · 0 0

fedest.com, questions and answers