Question:

How do i make this simple program in C?

by  |  earlier

0 LIKES UnLike

i just started C programing and i was wondering how i could this program:

the program will ask the user how many coins he/she has and how much money is needed. then the program will calculate if it is possible to make that change with the amount of coins that the user has the program will print out how many coins of each type the user will need.

Note that the change doesn't always have to be made with all of the coins.

 Tags:

   Report

2 ANSWERS


  1. First, you'll need to get the input for each of the coins you wish:

    >How many Quarters?  2<

    (repeat for dimes, nickels, and pennies.)

    Second, you'll need to get the cost of the item

    To do those two steps, look into printf and scanf.  They're kind of tricky, but you're sure to find examples out there.

    Next, you need to see if the cost is greater than a quarter. If it is, start subtracting quarters out of the total number of quarters.

    total_cost -= 25;

    number_quarters--;

    Do that inside of a while loop that that checks if the total_cost is greater than 25 and if you have quarters.

    Repeat that step for dimes (10) nickels (5) and pennies(1).

    The idea is that you're trying to subtract the value of the coins from the amount of change required.

    If you get to the end of the program (checking quarters, dimes, nickels, and pennies) and you still have a value in total_cost, then you don't have the correct change to make the cost.

    ----

    There's the functional design.  That should get you past worrying about how to write the program into the actual act of learning C.


  2. I don't know C, but here's the mechanics (given in C++) of how to do it

    #include <iostream>

    using namespace std;

    int main()

    {

        label:

        int p=0;

        int n=0;

        int d=0;

        int q=0;

        int hd=0;

        int pennies;

        int nickels;

        int dimes;

        int quarters;

        int halfDollars;

        int total;

        cin>>pennies;

        cin>>nickels;

        cin>>dimes;

        cin>>quarters;

        cin>>halfDollars;

        cin>>total;

        

        for (int HD=0; HD<halfDollars && (hd+1)*50<total;HD++){

            hd=hd+1;

            cout<<hd<<" half dollars \n";

            cout<<total-(hd*50+q*25+d*10+n*5+p)<<" remains\n";

        }

        if ((hd*50+q*25+d*10+n*5+p)<total){

           for (int Q=0; Q<quarters && (hd)*50+(q+1)*25<total;Q++){

               q=q+1;

               cout<<q<<" quarters \n";

               cout<<total-(hd*50+q*25+d*10+n*5+p)<<" remains\n";

           }

        }

        if ((hd*50+q*25+d*10+n*5+p)<total){

           for (int D=0; D<dimes && (hd)*50+(q)*25+(d+1)*10<total;D++){

               d=d+1;

               cout<<d<<" dimes \n";

               cout<<total-(hd*50+q*25+d*10+n*5+p)<<" remains\n";

           }

        }

        if ((hd*50+q*25+d*10+n*5+p)<total){

           for (int N=0; N<nickels && (hd)*50+(q)*25+d*10+(n+1)*5+p<total;N++)...

               n=n+1;

               cout<<n<<" nickels\n";

               cout<<total-(hd*50+q*25+d*10+n*5+p)<<" remains\n";

           }

        }

        if ((hd*50+q*25+d*10+n*5+p)<total){

           for (int P=0; P<pennies && (hd)*50+(q)*25+d*10+n*5+(p+1)<total;P++)...

               p=p+1;

               cout<<p<<" pennies\n";

               cout<<total-(hd*50+q*25+d*10+n*5+p)<<" remains\n";

           }

        }

        if ((hd*50+q*25+d*10+n*5+p)==total){

           cout<<p<<" pennies + "<<n<<" nickels + "<<d<<" dimes + "<<q<<" quarters + "<<hd<<" halfdollars gives "<<total;

        }

        else {

             cout<<"can't be made";

        }

        

        goto label;

    }

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions