IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 1: [C] – Data Types, Operators, Conditional Statements, Loops, and Command Line › Cash project takeaways
- This topic is empty.
-
AuthorPosts
-
August 27, 2022 at 6:14 am #486
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include <stdio.h> #include <cs50.h> int entercents(void); int count25(int b); int remainingafter25(int b); int count10(int b); int remainingafter10(int b); int count5(int b); int remainingafter5(int b); int count1(int b); int main(void) { int b = entercents(); printf("result is %i\n", b); int c25 = count25(b); printf("count25 is%i", c25); int r25 = remainingafter25(b); printf("remaining after 25count is %i", r25); b = r25; int c10 = count10(b); printf("count10 is %i", c10); int r10 = remainingafter10(b); printf("remaining after 10count is %i", r10); b = r10; int c5 = count5(b); printf("count5 is %i", c5); int r5 = remainingafter5(b); printf("remaining after 5count is %i", r5); b = r5; int c1 = count1(b); printf("count1 is %i", c1); int totalcoins = c25 + c10 + c5 + c1; printf("totalcoins %i", totalcoins); } int entercents(void) { int d; do { d = get_int("enter cents"); } while (d < 0); return d; } int count25(int b) { int x = b / 25; return x; } int remainingafter25(int b) { int x = b % 25; return x; } int count10(int b) { int x = b / 10; return x; } int remainingafter10(int b) { int x = b % 10; return x; } int count5(int b) { int x = b / 5; return x; } int remainingafter5(int b) { int x = b % 5; return x; } int count1(int b) { int x = b / 1; return x; }
[/dm_code_snippet]
Cash project will help learn creating functions in C. Once you execute the function for 25 cents successfully, replicate the same for 10, 5, and 1 cents. For 1 cents, there is no need to apply division and modulus operator (though I have applied this to maintain uniformity) as say remaining cents after 5 cents is 4 cents. So 4 cents will be the count of the number of coins.
[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.