# credit problem(pset 1) cs50

iam a beginner to programming and recently started cs50,in week1 there is a credit problem which stats: Implement a program that determines whether a provided credit card number is valid according to Luhn’s algorithm. though the program is getting compiled and is executed the output is not what i desired,it would be really helpful if anyone can point out the mistakes.

``````    #include<stdio.h>
#include<cs50.h>

int main(void)
{
int count=0,mod,sum1=0,sum2=0,sum=0;
long c,h;
printf("ENTER THE CREDIT CARD NO");
c=get_long_long();
h=c;
while(h!=0)
{
h=h/10;
count=count+1;

}

for(int i=0;i<count;i++)
{
mod[i]=c%10;
c=c/10;
}

for(int i=1;i<count;i+=2)
{
mod[i]=(mod[i]*2);
sum1=sum1+(mod[i]/10)+(mod[i]%10);
sum2=sum2+mod[i-1];
}
sum=sum1+sum2;
int a=mod[count-1];
int b=mod[count-2];

if(count==15)
{
if(a==3)
{
if(b==4||b==7)
{
if((sum%10)==0)
printf("your credit card is a valid american express card");
else
printf("your credit card is an invalid american express card");

}
}
}
if(count==13||count==16)
{
if(a==4)
{
if((sum%10)==0)
printf("your credit card is a valid visa card");

else
printf("your credit card is an invalid visa card");
}
}

if(count==16)
{
if(a==5)
{
if((b>=1)&&(b<=5))
{
if((sum%10)==0)
printf("your credit card is a valid  mastercard");

else
printf("your credit card is an invalid master card");

}
}

}

}
``````

Your digit counter "destroys" `c`, so there's nothing left for the next for loop. Create a copy to destroy instead. Or combine the two loops.
You shouldn't change the digits in `mod`, as you still need them in your if-else monster (which always should print exactly one word). Consider `&&` to reduce the nesting levels to one or two. `&&` has higher precedence than `||`, so chances are you might have to add parentheses.
• In the code you posted, `h` never receives a value. And with this arbitrary (not controlled by you) value you start into the loop. May 15, 2018 at 13:05
• Are there code paths through the code that don't print anything? You need to print exactly one word (like `"VISA"` or `"INVALID"`). And your checksum code ignores the first digit if the number has an odd number of digits. May 15, 2018 at 13:25
• Oh, and maybe you should begin debugging. This could be `eprintf` statements (`eprintf` is defined in cs50 library and works like printf), or defining a breakpoint and using `debug50` to step through your code. May 15, 2018 at 13:29