after 2 days and half i did it it's tricky at first but this problem made me understand loops and array much deeper , the question is my code is good as style and design ? any ideas should consider
#include<stdio.h>
#include<cs50.h>
#include<math.h>
unsigned long long get_long_long(string prompt);
int main(void)
{
unsigned long long card = get_long_long("Number:");
unsigned long long card1 = card ;
int sum, sum1, counter = 0 ;
while (card1 != 0)
{
card1 = card1 / 10 ;
counter++ ;
}
if (counter != 15 && counter != 16)
{
printf("INVALID\n") ;
}
int cc[counter];
for (int i = 0; i < counter; i++)
{
cc[i] = card % 10 ;
card = card / 10 ;
}
int cc1[counter];
for (int i = 0; i < counter; i++)
{
cc1[i] = cc[i];
}
for (int i = 1; i < counter; i += 2)
{
cc1[i] = cc1[i] * 2 ;
if (cc1[i] >= 10)
{
for (int j = 0; j < 2; j++)
{
int first_degit = cc1[i] / 10 ;
int secnd_degit = cc1[i] % 10 ;
cc1[i] = first_degit + secnd_degit ;
}
}
sum = sum + cc1[i] ;
}
for (int i = 0; i < counter; i += 2)
{
sum1 = sum1 + cc1[i] ;
}
int sum2 = sum1 + sum ;
sum2 = sum2 % 10 ;
if (counter == 15)
{
if (sum2 == 0)
{
if (cc[14] == 3 && cc[13] == 7)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
if (counter == 16)
{
if (sum2 == 0)
{
if ((cc[15] == 5 && cc[14] == 5) || (cc[15] == 5 && cc[14] == 1) || (cc[15] == 5 && cc[14] == 3))
{
printf("MASTERCARD\n");
}
else if (cc[15] == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
}
unsigned long long get_long_long(string prompt)
{
long long n ;
do
{
n = get_long("%s", prompt) ;
}
while (n < 1) ;
return n ;
}