Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:08:53+00:00 2026-05-28T00:08:53+00:00

I have generated code that counts the minimum number of 20’s, 10’s, 5’s, 2’s

  • 0

I have generated code that counts the minimum number of 20’s, 10’s, 5’s, 2’s and 1’s that will add up to a user defined amount of money. The user is only allowed to enter whole numbers i.e. no decimal values. I have two questions.

  1. If a denomination is not needed, the program outputs a random number instead of 0. How can I fix this?
  2. Is it possible to create a function that could replace all of the if statements and possibly the printf statements? I’m new to functions so am a little bit lost with them.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
 int pounds;
 int one, two, five, ten, twenty;

    printf("Enter a pounds amount with no decimals, max E999999: \n");
    scanf("%d", &pounds);
    printf("%d\n", pounds);

    if(pounds >= 20)
    {
        twenty = (pounds / 20);
        pounds = (pounds-(twenty * 20));
        printf("%d\n", pounds);
    }
    if(pounds >= 10)
    {
        ten = (pounds / 10);
        pounds = (pounds-(ten * 10));
        printf("%d\n", pounds);
    }
    if(pounds >= 5)
    {
        five = (pounds / 5);
        pounds = (pounds-(five * 5));
        printf("%d\n", pounds);
    }
    if(pounds >= 2)
    {
        two = (pounds / 2);
        pounds = (pounds-(two * 2));
        printf("%d\n", pounds);
    }
    if(pounds >= 1)
    {
        one = (pounds / 1);
        pounds = (pounds-(one * 1));
        printf("%d\n", pounds);
    }


 printf("The smallest amount of denominations you need are: \n");
 printf("20 x %d\n", twenty);
 printf("10 x %d\n", ten);
 printf("5 x %d\n", five);
 printf("2 x %d\n", two);
 printf("1 x %d\n", one);

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:08:53+00:00Added an answer on May 28, 2026 at 12:08 am

    This is a great example of why you should initialize your variables when you declare them.

    If pounds<20, then twenty will never get initialized. In C, variables have a (basically) random value assigned to them until you replace it with something else.

    You just need to do this:

    int one = 0, two = 0, five = 0, ten = 0, twenty = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of PHP code that is auto-generated and it puts all
I have the following SVG source code that generates a number of boxes with
I have some code that generates image of a pie chart. It's a general
I have a code that generates errors on my PC but does't on other
We have a project that generates a code snippet that can be used on
I have some PHP code that generates dynamic tables of data on the fly.
I have some LINQ code that generates a list of strings, like this: var
I have some C# code that generates google maps. This codes looks at all
I have some PHP code that generates a calendar and then outputs html to
I have some C code that parses a file and generates another file of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.