CS50x threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 1: [C] – Data Types, Operators, Conditional Statements, Loops, and Command Line › Credit card project bypasses use of arrays by making use of division and modulus operators: A quick observation
 This topic is empty.

AuthorPosts

August 29, 2022 at 12:57 pm #493
When confronted with the credit card project for the first time, it might appear that this project is imposed without first equipping with the knowledge of arrays. After all, one might wonder how to locate the first or last digit of entered credit card number without making use of arrays. This project bypasses use of arrays (a topic covered in the next week) by making use of division and modulus operators.
There is extensive usage of Boolean logic. For instance:
int f = checkforLuhn(y); if (f == 1) { LUHN = 1; }
It is perhaps only by hit and trial that you finally create functions like checkforLuhn.
Here is the completed project code (of course, there are a number of ways to approach such projects):
#include <stdio.h> #include <cs50.h> long enterccno(void);//long type as 14digit credit card will not fit into the memory as int type int countdigit(long y); int checkifAmexstart(long y); int checkifVisastart(long y); int checkifMcstart(long y); int checkforLuhn(long y); int main(void) { long y = enterccno(); long cc = countdigit(y); int AMEX = 0; int VISA = 0; int MASTERCARD = 0; int LUHN = 0; if (cc == 15) { int z = checkifAmexstart(y); int v = checkforLuhn(y); if (z == 1 & v == 1) { AMEX = 1; printf("AMEX\n"); return 0; } } if (cc == 13  cc == 16) { int z = checkifVisastart(y); int v = checkforLuhn(y); if (z == 1 & v == 1) { VISA = 1; printf("VISA\n"); return 0; } } if (cc == 16) { int z = checkifMcstart(y); int v = checkforLuhn(y); if (z == 1 & v == 1) { MASTERCARD = 1; printf("MASTERCARD\n"); return 0; } } int f = checkforLuhn(y); if (f == 1) { LUHN = 1; } if (cc < 13  cc == 14  cc > 16  VISA != 1  MASTERCARD ! = 1  AMEX ! = 1  LUHN ! = 1) { printf("INVALID\n"); } } long enterccno(void) //It will take void as argument as the function will not initially take any particular long value as input to begin with. { long x = get_long("enter creditcardno");//get_long function by cs50.h leveraged to accept input of creditcard no. from user. return x;//stored x value or entered creditcardno transferred inside the main function when entercccno function called. } int countdigit(long y) { long x = y; int c = 0; do { x = x / 10; c++; } while (x > 0); return c; } int checkifAmexstart(long y) { long x = y; if (x / 10000000000000 == 34  x / 10000000000000 == 37) { return 1; } else { return 0; } } int checkifMcstart (long y) { long x = y; if (x / 100000000000000 == 51  x / 100000000000000 == 52  x / 100000000000000 == 53  x / 100000000000000 == 54  x / 100000000000000 == 55) { return 1; } else { return 0; } } int checkifVisastart(long y) { long x = y; if (x / 1000000000000 == 4  x / 1000000000000000 == 4) { return 1; } else { return 0; } } int checkforLuhn(long y) { long x = y; int sum1 = 0; while (x > 0) { x = x / 10; int t = (x % 10) * 2; if (t > 9) { t = t % 10; sum1 = sum1 + t + 1; } else { sum1 = sum1 + t; } x = x / 10; } long w = y; int sum2 = 0; while (w > 0) { int u = w % 10; w = w / 10; sum2 = sum2 + u; w = w / 10; } int sum = sum1 + sum2; if (sum % 10 == 0) { return 1; } else { return 0; } }
[learn_press_profile]

AuthorPosts
 You must be logged in to reply to this topic.