I am trying not to use Arrays to solve this problem. However, the answer keeps showing as 0. Can someone pls help to explain?

Thanks a lot in advance!

Below is the code: I am trying 1. get the length of the credit card number 2. sum from the last digit up, i, i+2, i+4, until i < length of credit card number 3. sum from the second last digit up... But i am already stuck at step 2

int count=0;
while (number>0)

int sum1=0;
int sum2=0;    
int i;  
for (i=0;i<count;i+=2)   
    sum1+=number % 10;
    number= number/10;
printf("%i\n", sum1);   

1 Answer 1


The while loop divides number by 10 repeatedly until it is 0. That means that when the code drops down to the for loop, number will always be 0. The original value of number is lost after the while loop. The code needs to preserve the original value of number somewhere.

  • i got it, thank you very much for your explanation Cliff B!
    – cs50fan
    Commented Mar 10, 2020 at 2:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .