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 › Unable to print the total of all digits as part of solving credit card problem
Tagged: accepting input for credit card no, C programming language, classification into Amex/Visa/Mastercard., credit problem, cs50 debugging, debugging, no difference between the %i and %d format specifiers for printf, only use long type where applicable, problem sets, temporary printf statements
- This topic is empty.
-
AuthorPosts
-
October 10, 2021 at 6:38 am #44
Which part of the problem are you working on?
Summing the digits
What problem are you having?
#include <stdio.h> #include <cs50.h> int main(void) { long creditcardno; do { creditcardno = get_long("enter"); } while (creditcardno < 1000000000000 || creditcardno > 9999999999999999); int i = 0; long cc = creditcardno; while (cc > 0) { cc = cc / 10; i++; } printf("%d", i); if (i == 14) printf ("invalid"); return 0; int sum1 = 0; int mod1 = 0; int mm = creditcardno; do {mod1 = mm%10; mm = mm/10; sum1 = sum1 + mod1; } while (mm > 0); printf ("number%d", sum1); }
While the program seems to be working for accepting input for credit card no. if only the digits are between 12 to 15 digits, and giving output of invalid if 14 digits entered, I am unable to get the sum of all the digits as part of solving the next leg of the problem.
Reply
https://edstem.org/us/courses/176/discussion/667982?answer=1606799[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.