Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to write a C++ program which convert amount of money into amount of coins in

ID: 3562102 • Letter: H

Question

How to write a C++ program which convert amount of money into amount of coins in quarters, dime, nickle and penny ?

This is what i did but it said change from float to int possible data loss

#include <iostream>

using namespace std;

int main ( )
{
float userNUmber;
int change, quarters, dimes, nickels, pennies; // declare variables
cout <<"Enter the amount of money: ";
cin >> userNUmber; // input the amount of change
change = userNUmber * 100;
quarters = change / 25; // calculate the number of quarters
change = change % 25; // calculate remaining change needed
dimes = change / 10; // calculate the number of dimes
change = change % 10; // calculate remaining change needed
nickels = change / 5; // calculate the number of nickels
pennies = change % ; // calculate pennies

cout << " Quarters: " << quarters << endl; // display # of quarters
cout << " Dimes: " << dimes << endl; // display # of dimes
cout << " Nickels: " << nickels << endl; // display # of nickels
cout <<" Pennies: " << pennies << endl; // display # of pennies
system("Pause");
return (0);
}

Explanation / Answer

in the above case int has more precision value than float...so there might be an error while typecasting...exact number can't be copied in float type....so better use long int instead of float

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote