Answer:
#include<stdio.h>
#define DOLLAR 100
#define QUARTER 25
#define DIME 10
#define NICKEL 5
#define PENNY 1
void calculatedChange(int userAmount,int coinValues[])
{
if (userAmount >=100)
{
coinValues[0]=userAmount/DOLLAR;
userAmount=userAmount-(100*coinValues[0]);
}
if (userAmount >=25)
{
coinValues[1]=userAmount/QUARTER;
userAmount=userAmount-(25*coinValues[1] );
}
if (userAmount >=10)
{
coinValues[2]=userAmount/DIME;
userAmount=userAmount-(10*coinValues[2]);
}
if (userAmount >=5)
{
coinValues[3]=userAmount/NICKEL;
userAmount=userAmount-(5*coinValues[3]);
}
if (userAmount >=1)
{
coinValues[4]=userAmount/PENNY;
userAmount=userAmount-coinValues[4];
}
}
int main() {
int amount;
printf("Enter the amount in cents :");
scanf("%d",&amount);
if(amount<1)
{
printf("No change..!");
}
else
{
int coinValues[5]={0,0,0,0,0};
calculatedChange(amount,coinValues);
if (coinValues[0]>0)
{
printf("%d Dollar",coinValues[0]);
if(coinValues[0]>1) printf("s");
}
if (coinValues[1]>0)
{
printf(" %d Quarter",coinValues[1]);
if(coinValues[1]>1) printf("s");
}
if (coinValues[2]>0)
{
printf(" %d Dime",coinValues[2]);
if(coinValues[2]>1) printf("s");
}
if (coinValues[3]>0)
{
printf(" %d Nickel",coinValues[3]);
if(coinValues[3]>1) printf("s");
}
if (coinValues[4]>0)
{
printf(" %d Penn",coinValues[4]);
if(coinValues[4]>1) printf("ies");
else printf("y");
}
}
}
Explanation:
- Create a calculatedChange method for calculating userAmount.
-
Use conditional statements to check dollars
, quarters
, dimes
, nickels and penny.
- Inside the main method
, validate the input and then print dollars
, dimes
, nickels after checking them.