make_change.c

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int amount;        /* how much change the user has */
	int leftover;      /* used to hold amount as it decreases */
	int numquarters;   /* how many quarters */
	int numdimes;      /* how many dimes */
	int numnickels;    /* how many nickels */
	int numpennies;    /* how many pennies */
	
	/* read in the amount and save it */
	printf("Enter amount of change: ");
	if (scanf("%d", &amount) != 1 || amount < 0){
		/* oops ... bad input! */
		printf("Amount must be nonnegative integer\n");
		return(EXIT_FAILURE);
	}
	leftover = amount;
	
	/* computer the number of quarters and how much is left over */
	numquarters = leftover / 25;
	leftover = leftover % 25;
	
	/* computer the number of dimes and how much is left over */
	numdimes = leftover / 10;
	leftover = leftover % 10;
	
	/* computer the number of nickels and how much is left over */
	numnickels = leftover / 5;
	leftover = leftover % 5;
	
	/* whatever is left over is the number of pennies */
	numpennies = leftover;
	
	/* print the result */
	printf("%d cents is %d quarters, %d dimes, %d nickels, and %d pennies\n",
		amount, numquarters, numdimes, numnickels, numpennies);

	/* thank you, and goodnight! */
	return(EXIT_SUCCESS);
}



UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh