write a programe that read amount of money ranging from rm0.10 up to rm5.00(multiple of 10cents). Display number of coins that can be used tp form the above amount.

eg. input=rm1.40 , output: 50cents=2, 20cents=2, 10cents=0

Dani AI

Generated

asked for a program that breaks an RM amount (0.10–5.00 in 0.10 steps) into counts of 50c, 20c and 10c. asked for clarification and hinted about input — here is a short, robust plan: parse the input as a string and convert to integer cents (avoid floating-point rounding), validate it is between 10 and 500 cents and divisible by 10, then use a simple greedy division by 50, 20 and 10 to get counts.

Example C++ implementation (parses "RM1.40", "1.40", "2", etc.):

#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string line;
    if(!std::getline(std::cin, line)) return 0;

    // keep only digits and dot
    std::string t;
    for(char c: line) if((c >= '0' && c <= '9') || c == '.') t.push_back(c);
    if(t.empty()) { std::cout << "Invalid input\n"; return 0; }

    size_t p = t.find('.');
    int cents = 0;
    try {
        if(p == std::string::npos) {
            cents = std::stoi(t) * 100;
        } else {
            int dollars = (p==0) ? 0 : std::stoi(t.substr(0,p));
            std::string frac = t.substr(p+1);
            while(frac.size() < 2) frac.push_back('0');
            if(frac.size() > 2) frac = frac.substr(0,2);
            cents = dollars * 100 + std::stoi(frac);
        }
    } catch(...) { std::cout << "Invalid input\n"; return 0; }

    if(cents < 10 || cents > 500 || (cents % 10) != 0) {
        std::cout << "Invalid amount (must be 0.10 - 5.00 in 0.10 steps)\n";
        return 0;
    }

    int c50 = cents / 50; cents %= 50;
    int c20 = cents / 20; cents %= 20;
    int c10 = cents / 10;

    std::cout << "50c=" << c50 << ", 20c=" << c20 << ", 10c=" << c10 << "\n";
    return 0;
}

Notes and pitfalls: parsing as integers prevents floating-point precision errors that appear when using double. The greedy approach is safe here: replacing a 50c coin with smaller coins always needs at least three coins (20+20+10), so maximizing 50c first minimizes total coins. Validate user input (non-numeric, out of range, or not a multiple of 10) and give a clear error message. Example test: input "2.30" should yield 50c=4, 20c=1, 10c=1.

Recommended Answers

All 2 Replies

okay, what's your question about the problem

cin>> int variable;
now your turn

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.