-
-
Save mitrnsplt/10945596 to your computer and use it in GitHub Desktop.
| /** | |
| * | |
| * credit.c | |
| * | |
| * Peter Downs | |
| * | |
| * A program for validating credit card numbers. | |
| * | |
| */ | |
| #include <cs50.h> | |
| #include <math.h> | |
| #include <stdio.h> | |
| int main(void) | |
| { | |
| // Declare and initialize a variable and ask for user input. | |
| long long cardnumber = 0; | |
| // Ask for credit card number | |
| do | |
| {printf("What is your card number? "); | |
| cardnumber = GetLongLong(); | |
| } | |
| while (cardnumber < 0); | |
| // Determine whether it has a valid number of digits | |
| int count = 0; | |
| long long digits = cardnumber; | |
| while (digits > 0) | |
| { | |
| digits = digits/10; | |
| count++; | |
| } | |
| if ((count != 13) && (count != 15) && (count != 16)) | |
| { | |
| printf("INVALID\n"); | |
| } | |
| int number[count]; | |
| for (int i = 0; i < count; i++) | |
| { | |
| number[i] = cardnumber % 10; | |
| cardnumber = cardnumber / 10; | |
| } | |
| int originalnumber[count]; | |
| for (int i = 1; i < count; i++) | |
| { | |
| originalnumber[i] = number[i]; | |
| } | |
| for (int i = 1; i < count; i+=2) | |
| { | |
| number[i] = number[i] * 2; | |
| } | |
| int v = 0; | |
| int temp; | |
| if (count == 13) | |
| { | |
| for (int i = 0; i < count; i++) | |
| { | |
| temp = (number[i] % 10) + (number[i]/10 % 10); | |
| v = v + temp; | |
| } | |
| if (originalnumber[12] == 4 && v % 10 == 0) | |
| { | |
| printf("VISA\n"); | |
| } | |
| else | |
| { | |
| printf("INVALID\n"); | |
| } | |
| } | |
| if (count == 15) | |
| { | |
| for (int i = 0; i < count; i++) | |
| { | |
| temp = (number[i] % 10) + (number[i]/10 % 10); | |
| v = v + temp; | |
| } | |
| if (originalnumber[14] == 3 && v % 10 == 0 && (originalnumber[13] == 4 || originalnumber[13] == 7)) | |
| { | |
| printf("AMEX\n"); | |
| } | |
| else | |
| { | |
| printf("INVALID\n"); | |
| } | |
| } | |
| if (count == 16) | |
| { | |
| for (int i = 0; i < count; i++) | |
| { | |
| temp = (number[i] % 10) + (number[i]/10 % 10); | |
| v = v + temp; | |
| } | |
| if (originalnumber[15] == 4 && v % 10 == 0) | |
| { | |
| printf("VISA\n"); | |
| } | |
| else if (originalnumber[15] == 5 && v % 10 == 0 && (originalnumber[14] == 1 || originalnumber[14] == 2 || originalnumber[14] == 3 || originalnumber[14] == 4 || originalnumber[14] == 5)) | |
| { | |
| printf("MASTERCARD\n"); | |
| } | |
| else | |
| { | |
| printf("INVALID\n"); | |
| } | |
| } | |
| return 0; | |
| } |
This should be easier to understand. I placed each check in it's own function, so you can isolate which particular problem you might be struggling with. Cheers!
https://gist.github.com/Adeniyii/668fa47cedbcbfbaecb93b7686e0c4b5
Could someone explain this part to me? After dividing your credit card by 10 wouldn't you get a decimal. How can you do the modulo 10 of a decimal?
for (int i = 0; i < count; i++)
{
number[i] = cardnumber % 10;
cardnumber = cardnumber / 10;
}
how do i need to run in the terminal?
Could someone explain this part to me? After dividing your credit card by 10 wouldn't you get a decimal. How can you do the modulo 10 of a decimal?
for (int i = 0; i < count; i++)
{
number[i] = cardnumber % 10;
cardnumber = cardnumber / 10;
}
The cardnumber variable was initialized as a long, when a real number is stored as a long the decimal part gets cut off. So, you're actually calling modulo on an integer not a decimal. Hope that helps.
So i'm going through this now and I'm noticing that the vast majority of propopsed solutions are making use of arrays when they haven't even been taught yet (this is a week 1 pset)... I was under the impression that students should be using the methods taught so far to solve this problem??
This doesn't even require arrays lmao. I did this in both C and Python. (Using a very crummy C way to do it, didn't make it pythonic because iI used python as a psuedocode tool). Python may make it easier to understand what is going on (essentially psuedocode for C)
https://github.com/PPLSplit/misc/blob/master/CreditPyVer.py
Could someone explain this part to me? After dividing your credit card by 10 wouldn't you get a decimal. How can you do the modulo 10 of a decimal?
for (int i = 0; i < count; i++)
{
number[i] = cardnumber % 10;
cardnumber = cardnumber / 10;
}
In C, division is integer division by default. In other languages like python, you have a float division ( / operator ) and int division ( // operator) built in. Integer division just means instead of including decimals or rounding. you truncate the number. For example, 10/3 isn't 3.33333 but 3 and so forth.
is there any way of implementation the logic without using arrays?
Yes check this
problem-1-solution without array
https://github.com/me50/DeHacker17/blob/cs50/problems/2020/x/credit/credit.c
trying to check how i can get you solution without using arrays but page not found.
You can check my solution
problem-1-solution without array
For those who are still beginners with arrays, they are cleverly introduced in the following week's lecture (CS50 Week 2):
https://www.youtube.com/watch?v=v7Ho89RMRIo&t=851
Above solutions will make much more sense after watching.
Mate, if you could leave any comments for your code that would be nice.
after line 42 they are needed so bad.
I did differently without any help, I feel my version is lot easier to understand and less complicated
https://gist.github.com/aryan348/d390ec7aa4441ee086846516267cde4d