# How to make my code more efficent

• ### Question

• Hi everyone i just challenged a problem and completed it succeusfully

The problem is as follows,

ChangeMaker (This one tends to be tricky, it personally made me question whether or not I could be a programmer. Take your time and try to enjoy the learning process.)

• Prompt the use to enter an amount of dollars and cents. For example \$1.18. Display the number of quarters, dimes, nickels, and pennies to make that amount.

• Example: If the user entered \$1.18 it should output: 4 quarters, 1 dimes, 1 nickels, 3 pennies

• If the user entered \$1.02 it should output: 4 quarters, 0 dimes, 0 nickels, 2 pennies

My solution is below , and in works , but i was wondering is there any where to make this better ?

```using System;

public class Program
{
public static void Main()
{
Program n =new Program();
Console.WriteLine("Welcome to the currency converter");
}

public void moneychange (double x)
{
int numofquarters=0;
int numofdimes=0;
int numofnickels=0;
int numofpennies=0;
int valueincents= Convert.ToInt32(x * 100);

if(valueincents >= 25)
{
numofquarters=valueincents / 25;
int remainder = valueincents % 25;
if (remainder !=0)
{

if (remainder >= 10)
{
numofdimes=remainder / 10;
remainder = remainder % 10;
if (remainder !=0)
{
if (remainder >= 5)
{
numofnickels=remainder / 5;
remainder = remainder % 5;
if (remainder!=0)
{
if (remainder >=1)
{
numofpennies=remainder / 1;
remainder= remainder % 1;

Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
else
{
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
if (remainder >= 1)
{
numofpennies=remainder /1;
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
else
{
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
if (remainder >= 5)
{
numofnickels=remainder/5;
remainder= remainder % 5;
if( remainder !=0)
{
if (remainder >=1)
{
numofpennies=remainder / 1;
remainder= remainder % 1;

Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
else
{
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
if(remainder >=1)
{
numofpennies=remainder /1;
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
else
{
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}

}
else if (valueincents >= 10)
{
numofdimes=valueincents / 10;
int remainder = valueincents % 10;
if(remainder != 0)
{
if (remainder >= 5)
{
numofnickels=remainder / 5;
remainder = remainder % 5;
if (remainder!=0)
{
if (remainder >=1)
{
numofpennies=remainder / 1;
remainder= remainder % 1;

Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
else
{
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
if (remainder >=1)
{
numofpennies=remainder /1;
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);

}
}
else
{
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
else if ( valueincents >= 5)
{
numofnickels=valueincents / 5;
int remainder = valueincents % 5;
if(remainder !=0)
{
if (remainder >=1)
{
numofpennies=remainder / 1;
remainder= remainder % 1;

Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
else
{
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
else if (valueincents >=1)

{
numofpennies=valueincents/1;
Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}

}
}```

Thank you all much apperciated!

Tuesday, February 26, 2019 8:54 AM

### All replies

• Well, yes, there are ways to improve it.  All of those nested "if" statements are completely unnecessary.  All you have to do is start with the largest denomination and move on down:

```using System;

public class Program
{
public static void Main()
{
Console.WriteLine("Welcome to the currency converter");
}

public static void moneychange (double x)
{
int valueincents = Convert.ToInt32(x * 100);

int numofquarters = valueincents / 25;
valueincents = valueincents % 25;

int numofdimes = valueincents / 10;
valueincents = valueincents % 10;

int numofnickels = valueincents / 5;
valueincents = valueincents % 5;

int numofpennies = valueincents;

Console.WriteLine("{0} quarters, {1} dimes, {2} nickels, {3} pennies",numofquarters,numofdimes,numofnickels,numofpennies);
}
}
```

Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

Wednesday, February 27, 2019 7:14 AM