why my code is not printing out desired output. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why my code is not printing out desired output.

I wrote this code in C as a problem set from CS50, problem needed to be solved was to write a programme that can check if a credit card is valid and then if the card is valid identify ether the card is VISA,AMERICAN EXPRESS OR MASTERCARD. I wrote the algorithim to check if the card number is valid and is printing invalid if the card number is invialid. My problem is that my code doesn't wanna print out the name is card is card number is valid. here is my code: #include <cs50.h> #include <stdio.h> int main(void) { long card; //prompt the user for card number do { card = get_long("Card no: "); } while (card < 0); // card number must always be greater than zero //count digit starting from second-last then multiply by two int card1,card2,card3,card4,card5,card6,card7,card8; card1 = ((card % 100)/10 * 2); card2 = ((card % 10000)/1000 * 2); card3 = ((card % 1000000)/100000 * 2); card4 = ((card % 100000000)/10000000 * 2); card5 = ((card % 10000000000)/1000000000 * 2); card6 = ((card % 1000000000000)/100000000000 * 2); card7 = ((card % 100000000000000)/10000000000000 * 2); card8 = ((card % 10000000000000000)/1000000000000000 * 2); card1 = (card1 % 100 / 10) + (card1 % 10); card2 = (card2 % 100 / 10) + (card2 % 10); card3 = (card3 % 100 / 10) + (card3 % 10); card4 = (card4 % 100 / 10) + (card4 % 10); card5 = (card5 % 100 / 10) + (card5 % 10); card6 = (card6 % 100 / 10) + (card6 % 10); card7 = (card7 % 100 / 10) + (card7 % 10); card8 = (card8 % 100 / 10) + (card8 % 10); int sum1 = card1 + card2 + card3 + card4 + card5 + card6 + card7 + card8; //find digits that where not multiplyed by two int card9,card10,card11,card12,card13,card14,card15,card16; card9 = (card % 10); card10 = ((card % 1000)/100); card11 = ((card % 100000)/10000); card12 = ((card % 10000000)/1000000); card13 = ((card % 1000000000)/100000000); card14 = ((card % 100000000000)/10000000000); card15 = ((card % 10000000000000)/1000000000000); card16 = ((card % 1000000000000000)/100000000000000); int sum2 = card9 + card10 + card11 + card12 + card13

18th Jan 2023, 12:39 PM
Khulyso DevĀ®
Khulyso DevĀ® - avatar
5 Answers
+ 1
Khulyso DevĀ® Please dont post raw code in the description. Create a new C code in the playground, paste your code in there and then put the link to that code in the description instead. Then people can review the code much easier and help you faster.
18th Jan 2023, 12:44 PM
Tim G
Tim G - avatar
+ 2
Khulyso DevĀ® exactly. its more readable like that and there will also be syntax highlighting. It doesn't necessarily have to compile.
18th Jan 2023, 12:57 PM
Tim G
Tim G - avatar
0
you mean I should first write code on sololearn playground and then share a link, the one I posted above I wrote it using VS code
18th Jan 2023, 12:53 PM
Khulyso DevĀ®
Khulyso DevĀ® - avatar
0
Khulyso DevĀ® I think that you will have to revise either your method or else the data types. The maximum long int is 2147483647.
18th Jan 2023, 11:57 PM
Brian
Brian - avatar
- 1
int sum2 = card9 + card10 + card11 + card12 + card13 + card14 + card15 + card16; int sum3 = sum1 + sum2; int length = 0; long visa = card; long amex = card; long master = card; if((sum3 % 10)!= 0) { printf("INVALID\n"); return 0; } //Differetiate between visa,master and Amex while (card >0) { card = card / 10; length++; } //Identify if its VISA while (visa >= 0) { visa /= 10; } if (visa == 4 && (length == 13 || length == 16)) { printf("VISA\n"); return 0; } //identify if its Amex while (amex >= 10000000000000) { amex /= 10000000000000; } if (length == 15 && (amex == 34 || amex == 37)) { printf("AMEX\n"); return 0; } //identify if its mastercard while (master >= 100000000000000) { master /= 100000000000000; } if (length == 16 && (master == 51 || master == 52 || master == 53 || master == 54 || master == 55)) { printf("MASTERCARD\n"); return 0; } else printf("INVALID\n"); return 0; }
18th Jan 2023, 12:43 PM
Khulyso DevĀ®
Khulyso DevĀ® - avatar