-
-
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; | |
} |
Thanks for the assistance, very concise and easy to follow.
Hi. I did it differently but your solution is way more elegant.
My problem was that I didn't know how to make the program recognize the position of a diigit in the number and had to find a workaround.
Your "number[i]" looks like what I was looking for. Could you explain to me how you that works?
EDIT: I got it now, you transformed the number in an array of single digit numbers
Hi, can someone explain the reason for making the originalnumber[] array?
Specifically, it's a duplicate of number[] with the exception of never defining the character in the 0 position. Why is that?
If the only thing originalnumber[] is needed for later is to check at most the last two digits (first two of cardnumber), why not only assign those last two digits from number[]?
Hey, you have multiplied all alternate numbers by 2 but you have not added them and also haven't done the next step of adding the other alternate numbers.
Also line 43 instead of for (int i = 0; i < count; i++) it should be for (int i=count-1;i>=0;i--) as yours will store last digit of cc to 0th digit of number
Also line 50 it should be int i = 0; instead of int i = 1;
Can someone please explain to me very briefly specifically how the Checksum section works?
Would really appreciate that
So in your for loops between the lines 49 and 59, what is the point of declaring i = 1? Wouldn't that give you one less number in the originalnumber variable?
Thanks for the solution. May I ask what exactly digits = digits /10, count++ does? (Line 33,34) Thank you!
Basically this counts the number of digits in card number. Digits being divided by 10 unless digits become less than 0 i.e. if we divide 2354 by 10 (while loop) 1st time, we shall get 235 as quotient. 2nd time(count++) its 23 and so on unless it becomes<0. Hence 2354 is having 4 nos of digits (count).
Can any body explain me, what is happening in the 5th, 6th and 8th line of below code copied from above. Please
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");
}
}
5th line: adding the digits multiplied by 2 from second last digits and remaining digits (not multiplied by 2). Since we need to add the products digits and not the product themselves hence included (number[i] % 10) + (number[i]/10 % 10); i.e. if suppose we get 14 after multiplying the 4th digit by 2, than first part will be 4 (number[i] % 10) and second part will be 1(number[i]/10 % 10) i.e. 4+1= 5 (sum of 14).
6th line: it is the summation withing for loop (int i=0; i<count;i++)
8th line: this line basically checks the condition of visa card (if 1st digit is 4 and summation divided by 10 is 0 i.e. the remainder should be zero after divided by 10.
thanks I got a clue
is there any way of implementation the logic without using arrays?
Thanks for the solution. May I ask what exactly digits = digits /10, count++ does? (Line 33,34) Thank you!
this replaces the old variable of "digits" with a new value that removes the last number of the card and adds one to the counter. the counter "count" is used to take the actual card number and change it to how many digits long the card number is. for example "1259968426547" would have a "count" value of thirteen. this allows later code to be written to identify what type of card it is (master, amex, visa, invalid).
You do not need arrays for this problem
is there any way of implementation the logic without using arrays?
yes check out my solution
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.
Can any body explain me, what is happening in the 5th, 6th and 8th line of below code copied from above. Please
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");
}
}5th line: adding the digits multiplied by 2 from second last digits and remaining digits (not multiplied by 2). Since we need to add the products digits and not the product themselves hence included (number[i] % 10) + (number[i]/10 % 10); i.e. if suppose we get 14 after multiplying the 4th digit by 2, than first part will be 4 (number[i] % 10) and second part will be 1(number[i]/10 % 10) i.e. 4+1= 5 (sum of 14).
6th line: it is the summation withing for loop (int i=0; i<count;i++)8th line: this line basically checks the condition of visa card (if 1st digit is 4 and summation divided by 10 is 0 i.e. the remainder should be zero after divided by 10.
Thank you for explaining it ! I was struggling to understand what is going on in those lines:) @mohankholiya
I did differently without any help, I feel my version is lot easier to understand and less complicated
https://gist.github.com/aryan348/d390ec7aa4441ee086846516267cde4d
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.
Can any body explain me, what is happening in the 5th, 6th and 8th line of below code copied from above. Please
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");
}
}