I have broken the credit problem into 2 parts
- Checking whether the sum after the Luhn’s algorithm has a zero at the end or not
- If the inout integer is valid then go ahead and find the card type using switch.
for the 1st part have converted the integer into an array and then performed operations on odd and even numbers in the array to get the sum and check. But while running the code I'm getting the following errors
P.S - this my very first hands on experience at coding so please bear if this is a very native doubt.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
long long n;
int c = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
int numberArray[c];
while (n != 0)
{
numberArray[c] = n % 10;
n /= 10;
c++;
}
int sum_odd = 0;
int sum_even = 0;
int sum_total = 0;
for (int i = 1 ; i < c ; i+=2)
{
int x1 = 2* numberArray[i];
if (x1 >9)
{
x1 = x1%10 + x1%100 ;
}
sum_even = sum_even + x1 ;
}
for (int i = 0 ; i<c ; i+=2)
{
sum_odd = sum_odd + numberArray[i];
}
sum_total = sum_odd + sum_even;
if (sum_total % 10 != 0 )
{
printf ("Invalid\n");
}
}