I took the advice of some of you and googled college level homework assignments for c programming and was able to locate a few sources. Here is my very first program, written on my own, using my own logic and without any help.
Before you tear me up too much, please realize that I am sure this could be written much smaller and more efficiently. Your input is what I can use to learn not only to work with the logic of C, but the beginning of thinking on how to write more efficient code and how to write code in the most commonly accepted format.
If you want to lead me to a more compact design for this program, please try to hint your way around it so I can try and figure out what you mean. It will help me to gain a deeper understanding of the logic than if you come right out and tell me how to do something. Plus, if I get stumped...I will ask outright. LOL.
Thanks and double thanks for bearing with a newbie whom is trying to teach himself how to program.
Code:
/* develop a program that accepts a dollar amount (in cents) and then computes
the number of quarters, dimes, nickels, and pennies needed. The solution must
produce the smallest number of coins possible for the given dollar amount*/
#include <stdio.h>
main()
{
int c,q = 0, d = 0, n = 0, p = 0; /* I had to initialize my actual coin
counts to zero, else I got some pretty
amazing numbers*/
printf( "Please enter an amount ( in cents ):" );
scanf( "%d", &c );
getchar();
while ( c >= 25 ){
q++;
c = c - 25; /*Could I have used c -= 25 for this and following statements*/
}
printf( "* You will need %d quarters.\n", q);
while ( c >= 10 ){
d++;
c = c - 10;
}
printf( "* You will need %d dimes.\n", d);
while ( c >= 5 ){
n++;
c = c - 5;
}
printf( "* You will need %d nickels.\n", n);
while ( c >= 1 ){
p++;
c = c - 1;
}
printf( "* You will need %d pennies.\n", p);
getchar();
}