This is my first programming course in college (Intro to C). The programming ass
ID: 3675544 • Letter: T
Question
This is my first programming course in college (Intro to C). The programming assignment is about writing a program that converts a number of pennies to its larger coin equivalent. The
program should ask the user to enter an amount as a decimal number such a 4.66 for
4 hundred sixty-six pennies. The program should then report how many quarters,
dimes, nickels, and pennies it would take to make that amount of change.
Please enter the amount of money you have: 4.66
$4.66 is equivalent to:
Quarters: 18
Dimes: 1
Nickels: 1
Pennies: 1
Here is my sample code, however, I am having a hard time properly executing this program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numQuarters == 0.0;
int numDimes == 0.0;
int numNickels == 0.0;
int numPennies == 0.0;
printf("Please enter the amount of pennies you have: ", numPennies);
scanf("%f", &numPennies);
printf("%f is equivalent to: ",);
numQuarters == (numPennies * 100.00) / 25.00);
scanf("%f", &numQuarters);
printf("Quarters: %f " , numQuarters);
numDimes == (numPennies * 100.00) / 10.00;
scanf("%f", &numDimes);
printf("Dimes:%f ", numDimes);
numNickels == (numPennies * 100.00 / 5.00);
scanf("%f", &numNickels);
printf("Nickels:%f ", numNickels);
numPennies == (numPennies * 100.00 / 1.00);
scanf("%f", &numPennies);
printf("Pennies: %f ", numPennies);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <math.h>
int main() {
float totalMoneyFromUser; // money, input by user
int totalMoneyAsPennies; // money converted into pennies
int CS_Quaters = 0, CS_Dimes = 0; // amount of coins
int CS_Nickels = 0, CS_Pennies = 0; // money is converted into
const int quatersValue = 25, dimesValue = 10, nickelsValue = 5;
// Ask for user input
printf("Please enter the amount of money you have: ");
// Store the value in totalMoneyFromUser
scanf("%f", &totalMoneyFromUser);
// Convert the total into pennies so that coin amounts can be
// taken out most efficiently with bit shifting.
// O(n) #best# time complexity
totalMoneyAsPennies = (int)round(totalMoneyFromUser * 100);
CS_Quaters = floor(totalMoneyAsPennies / quatersValue);
int quarterReturn = totalMoneyAsPennies - CS_Quaters * quatersValue;
CS_Dimes = floor(quarterReturn / dimesValue);
int dimeReturn = quarterReturn - CS_Dimes * dimesValue;
CS_Nickels = floor(dimeReturn / nickelsValue);
CS_Pennies = floor(dimeReturn - CS_Nickels * nickelsValue);
// Multi-Line prints for clarity to display the results to the user.
printf(" "); // New line break
printf("$%.2f is equivalent to: ", totalMoneyFromUser);
printf("Quarters..: %d ", CS_Quaters);
printf("Dimes.....: %d ", CS_Dimes);
printf("Nickels...: %d ", CS_Nickels);
printf("Pennies...: %d ", CS_Pennies);
// Release memory
return (0);
}
sample output
Please enter the amount of money you have: $4.66
$4.66 is equivalent to:
Quarters..: 18
Dimes.....: 1
Nickels...: 1
Pennies...: 1
[Finished in 0.1s]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.