# pset1 - Problems with "greedy" - I've written my code, but there is no output

I used "while" loops and created 2 different codes, but when the first question is asked - "How much change is owed?" I give the input and there is no result. I'll post the two codes and I hope any of you guys gives me a tip about what I can change.

First code:

``````#include <stdio.h>
#include <cs50.h>

int main(void)

{
int penny = 1;
int nickel = 5;
int dime = 10;
int quarter = 25;

printf("How much change is owed?\n");

float t = GetFloat();

int q = t * 100;
int p;
int i;

do
{
p = 0;
i = q - p * quarter;
p = p + 1;
}
while (i > 0);

i = q % quarter;

int r;
int o;

do
{
r = 0;
o = i - r * dime;
r = r + 1;
}
while (o > 0);

o = i % dime;

int l;
int y;

do
{
l = 0;
y = o - l * nickel;
l = l + 1;
}
while (y > 0);

y = o % nickel;

int u;
int k;

do
{
u = 0;
k = y - u * penny;
u = u + 1;
}
while (k > 0);

int v = p + r + l + u;
float x = v * 0.01;
printf("%f\n", x);
}
``````

Second code:

``````#include <stdio.h>
#include <cs50.h>

int main(void)

{
int penny = 1;
int nickel = 5;
int dime = 10;
int quarter = 25;

printf("How much change is owed?\n");

float t = GetFloat();
int q = t * 100;

int p;
int i;

do
{
p = 0;
i = q - p * quarter;
p = p + 1;
if(p * quarter + quarter > q)
{
int r;
int o;

do
{
r = 0;
o = i - r * dime;
r = r + 1;
i = q % quarter;
if(r * dime + dime > i)
{
int l;
int y;

do
{
l = 0;
y = o - l * nickel;
l = l + 1;
o = i % dime;
if(l * nickel + nickel > o)
{
int u;
int k;

do
{
u = 0;
k = y - u * penny;
u = u + 1;
y = o % nickel;
if (u * penny + penny > y)
{
k = p + r + l + u;
float z = k * 0.01;
printf("%f\n", z);
}
}
while (k > 0);
}
}
while (y > 0);
}
}
while (o > 0);
}
}
while (i > 0);
}
``````

Thank you in advance. P.S. I would also like to specify that I will later try to figure out the negative input code that I need to add - right after I'm sure the rest is right.

You've created infinite loops. Look at the first version:

``````do
{
p = 0;
i = q - p * quarter;
p = p + 1;
}
while (i > 0);
``````

p is set to 0 at the beginning of each pass through the loop, so i is always equal to q which never changes. Since i never drops to 0, it's an infinite loop.

The second version is just a more complicated version of the same.

Hint: There's an old saying in computer programming, "KISS, Keep It Simple, Stupid!", something I remind myself of on just about every project. The point is to keep code as simple as possible. It's more efficient and less likely to introduce errors.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)