본문 바로가기
C and C++/C and C++ Examples

[C Examples] 달러를 동전으로 바꾸는 예제코드

by Henry Cho 2023. 2. 8.
728x90

달러를 동전으로 바꾸는 예제코드

포스트 난이도: HOO_Intern


# Example Codes

 

#include <stdio.h>

int main()
{
    int cents, halfDollars, quarters, dimes, nickels, pennies;
    printf("Enter change in [cents]: ");
    scanf("%d",&cents);
    printf("The change that has been entered is %d \n",cents);
    halfDollars = (cents*2)/100;
    printf("Half dollars: %d \n",halfDollars);
    cents=cents - ((halfDollars*100)/2);
    quarters=cents/25;
    printf("Quarters: %d \n", quarters);
    cents = cents - (quarters*25);
    dimes = cents/10;
    printf("Dimes: %d \n", dimes);
    cents = cents - (dimes*10);
    nickels = cents/5;
    printf("Nickels: %d \n", nickels);
    cents = cents - (nickels*5);
    pennies = cents;
    printf("Pennies %d \n", pennies);
    printf("Posted by HOO."); 

    return 0;
}

Enter change in [cents]: 1377
The change that has been entered is 1377 
Half dollars: 27 
Quarters: 1 
Dimes: 0 
Nickels: 0 
Pennies 2 
Posted by HOO.

 

728x90

댓글