# Thread: C Assign Help: Find the least amount of coins required to make a value with a twist

1. ## C Assign Help: Find the least amount of coins required to make a value with a twist

Hey guys, im new to the forums, and sort of new to programming in c. Im currently in my second semester of college so I already have some programming knowledge. Our assignment is this:

Write a program in C that will allow a user to enter an amount of money, then display the least number of coins and the value of each coin required to make up that amount. The twist to this program is the introduction of a new coin – the jonnie (value = \$1.26). Therefore, the coins available for your calculations are: Twonies, Loonies, Jonnies, Quarters, Dimes, Nickels and Pennies. You can assume the amount of money user enters will be less than \$100.00.

User enters \$4.53. Your output should be:
The least number of coins required to make up \$4.53 is 4. The coins needed are:

1 Toonie \$2.00
2 Jonnies \$2.52
1 Penny \$0.01
\$4.53

I'm wondering if i'm close. I'm stuck on what to do for the output. This is what i have so far. Any input on making this shorter is greatly appreciated. We can only use #include <stdio.h>

[tag]
Code:

Code:
```//Start

#include <stdio.h>

int main()
{
//Get user input for amount of money

//Input

int i;

//Store input from user

printf("Please enter an amount to find the least number of coins to\nmake up your value: ");
scanf("%.2d",i);

//Coin values avail for use

int t;
int j;
int l;
int q;
int d;
int n;
int p;

int total_coins;

//Calc the least amount of coins for an input < \$100.00

for (i = 0; i < 100;)
{

if (i >= 2)
{
t = i/2;
j = i % t / 1.26;
l = i % j / 1;
q = i % l / 0.25;
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;

total_coins = t + j + l + q + d + n + p;

getchar();
}
else if ((i < 2) && (i >= 1.26))
{
j = i % t / 1.26;
l = i % j / 1;
q = i % l / 0.25;
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;

total_coins = j + l + q + d + n + p;
getchar();
}
else if ((i < 1.26) && (i >= 1.00))
{

l = i % j / 1;
q = i % l / 0.25;
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;

total_coins = l + q + d + n + p;
getchar();

}
else if ((i < 1.00) && (i >= 0.25))
{

q = i % l / 0.25;
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;

total_coins = q + d + n + p;
getchar();

}
else if (( i < 0.25) && (i >= 0.10))
{
d = i % q / 0.1;
n = i % d / 0.05;
p = i % n / 0.01;

total_coins = d + n + p;
getchar();

}
else if ((i < 0.10) && (i >= 0.05))
{
n = i % d / 0.05;
p = i % n / 0.01;

total_coins = n + p;
getchar();

}
else if ((i < 0.05) && (i >= 0.01))
{

total_coins = i / 0.01;
getchar();

}

}
return 0;
}```
[/tag]

2. I'd convert dollar and cent to a number of cents. Say \$1.32 would be read as two integers (1 and 32 with a decimal point between them), and then combined into a result of 132 cents.

Then you can just use the modulo operator, with everything done with integral values (no mucking around with floating point - which is error prone for handling money - at all).

Then just use a sorted array with the value of each type of coin, and allocate largest coins first. I certainly wouldn't duplicate code the way you have.

Since a "jonnie" is not a multiple of other denominations (but it is a sum) you might try some tricks involving removing jonnies and/or larger denominations from consideration, and comparing the results you get.

3. Thanks so much! Definately makes more sense to me now.. I didn't think about converting the cents part. Just one question how do you sort the array for it though? I'm just starting on arrays in class so i'm somewhat familiar with them

4. You can always initialize it sorted from the largest denomination to the least. Your book will show you what array initialization looks like.