Create a function called totalCoins that takes an integer price of a product (in pennies) and an integer amount of money paid (in pennies). It should return via output parameters the number of dollars, quarters, dimes, nickels, and pennies given as change. Must use pointers. C programming
C Code:
#include<stdio.h>
//definition for totalCoins()
void totalCoins(int price, int amount)
{
//variables declaration
int change , dollors , quarters , dimes , nickels , pennies;
//change is the amount to be returned as change
change = amount - price;
dollors=change/100; //calculating dollors from change
//if dollor is valid or more than 0 than update remaining change value
if(dollors>=1)
change=change-dollors*100;
quarters=change/25; //calculating quarters from change
//if quarters is valid or more than 0 than update remaining change value
if(quarters>=1)
change=change-quarters*25;
dimes=change/10; //calculating dimes from change
//if dimes is valid or more than 0 than update remaining change value
if(dimes>=1)
change=change-dimes*10;
nickels=change/5; //calculating nickels from change
//if nickels is valid or more than 0 than update remaining change value
if(nickels>=1)
change=change-nickels*5;
//printing output...
printf("\nChange=>\nDollors: %d\nQuarters: %d\nDimes: %d\nnickels: %d\nPennies: %d",dollors,quarters,dimes,nickels,change);
}
//main
int main()
{
//variable declaration
int price , amount;
//taking inputs
printf("Enter price and amount: ");
scanf("%d%d",&price,&amount);
//function call
totalCoins(price ,amount);
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.