How to divide a given amount into dollar bills in C

2 Answers

0 votes
# include <stdio.h> 

int main()
{
    int amount = 893;
    
    printf("Amount are : %d\n", amount);

    if (amount >= 100) {
        printf("Number of hundreds are : %d\n", amount / 100);
        amount = amount % 100;
    }
    if (amount >= 50) {
        printf("Number of fifties are : %d\n", amount / 50);
        amount = amount % 50;
    }
    if (amount >= 20) {
        printf("Number of twenties are : %d\n", amount / 20);
        amount = amount % 20;
    }
    if (amount >= 10) {
        printf("Number of tens are : % d\n", amount / 10);
        amount = amount % 10;
    }
    if (amount >= 5) {
        printf("Number of fives are : %d\n", amount / 5);
        amount = amount % 5;
    }

    printf("Number of ones are : %d\n", amount);

    return 0;
}





/*
run

Amount are : 893
Number of hundreds are : 8
Number of fifties are : 1
Number of twenties are : 2
Number of ones are : 3

*/

 



answered May 13, 2023 by avibootz
0 votes
#include <stdio.h>

int main(void) {
    int bills[8] = { 500, 100, 50, 20, 10, 5, 2, 1 };
    int amount = 9476;

    int tmp = amount;
    for (int i = 0; i < 8; i++) {
        printf("%d = %d\n", bills[i], tmp / bills[i]);
        tmp = tmp % bills[i];
    }

    return 0;
}





/*
run:

500 = 18
100 = 4
50 = 1
20 = 1
10 = 0
5 = 1
2 = 0
1 = 1

*/

 



answered May 13, 2023 by avibootz
...