Created
May 12, 2024 17:31
-
-
Save thibaultseynaeve/37c7b7477e57567a6b29ae82cc7706c6 to your computer and use it in GitHub Desktop.
Harvard CS 50 - Week 1 (C) - Pset Cash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
int main(void){ | |
// Get integer (cents) from user | |
// Repeat this prompt until the user enters a value higher than 0 | |
int change; | |
do{ | |
change = get_int("Change owed: "); | |
} while (change < 0); | |
// Define possible coints | |
int coincounter = 0; | |
// We use module, this will calculate the remainder | |
// If the remainder is the same as the starting value, this means that we starting value is higher than the coin | |
// So we need to move to a lower coin | |
// We need to repeat this loop until the change is 0 | |
while (change != 0){ | |
if(change % 25 != change){ | |
// The remainder is not equal to the starting value | |
// So we can use the coin! | |
change = change - 25; | |
// Increment the coin counter | |
coincounter++; | |
} else if(change % 10 != change){ | |
// The remainder is not equal to the starting value | |
// So we can use the coin! | |
change = change - 10; | |
// Increment the coin counter | |
coincounter++; | |
} else if(change % 5 != change){ | |
// The remainder is not equal to the starting value | |
// So we can use the coin! | |
change = change - 5; | |
// Increment the coin counter | |
coincounter++; | |
} else if(change % 1 != change){ | |
// The remainder is not equal to the starting value | |
// So we can use the coin! | |
change = change - 1; | |
// Increment the coin counter | |
coincounter++; | |
} | |
} // End while loop | |
// Display the number of coins needed | |
printf("%i\n",coincounter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment