First ever program in C

First program I have ever created

Page 1 of 1

2 Replies - 623 Views - Last Post: 28 July 2008 - 06:29 PM Rate Topic: -----

#1 Dogmasur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-July 08

First ever program in C

Post icon  Posted 28 July 2008 - 04:00 PM

This was a problem I found when googling for college-level c programming homework assignments. It was done without any help and I am sure it can be compacted to be made more efficient. If you want to offer me advice on how to improve the logic or the format, please be indirect. That way I can try to flesh out what you mean on my own. This will help me gain a deeper understanding of the logic, the language and the generally accepted format for programming. If I get stumped...I'll ask outright. Hehe.

Thanks and bear with me. It's all new and I am teaching myself.




/* 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 use c -= 25 format here and
										  in 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();
}


This post has been edited by Dogmasur: 28 July 2008 - 06:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: First ever program in C

#2 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: First ever program in C

Posted 28 July 2008 - 05:45 PM

So what is your question? Also, while( c >=1 ){ should be while( c >=0){ I think

What are your errors? I'm not sure where to start looking. Have a good time with C/C++ :)
Was This Post Helpful? 0
  • +
  • -

#3 Dogmasur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-July 08

Re: First ever program in C

Posted 28 July 2008 - 06:29 PM

No questions. I just wanted to know how I did. I know I could use a function to sort out all of the arithmetic with the change, but am just beginning to learn about functions...so I'm not sure how to do that just yet. I probably could have used the modulus operator to make the arithmetic less redundant also.

As far as while ( c >= 1 ), maybe I could have used while ( c > 0 ). If I use while ( c >= 0 ) then, the way I have this program set up would add an extra penny at 0 wouldn't it?

Thanks for your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1