0

So I coded the first lines of the Luhn's Algorithm and tried to print the values of some of my variables to be sure they were doing exactly what I want it. For my surprise they weren't. And I were here thinking for hours trying to figure out what was wrong. The "printf" keeps returning a bunch of zeros as the values of the variables.

enter image description here

Let me know what you guys think about it and thanks for all the answers, have a good night. :)

So this is what I coded so far:

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

long number;
int count;
int digits;
int sum_1;
int sum_2;

int main(void)
{
    do
    {
        number = get_long("Number: "); //Ask the CC number.
    }
    while(number < 0); //If the user answer a negative value, ask again.

    while(number != 0) //Count the number of digits.
    {
        number /= 10; 
        count++;
    }
    for (digits = 1; digits <= count; digits++)
    {
        int ten_counter = pow(10, digits);
        
        if (digits % 2 == 0)
        {
            sum_1 += (number % ten_counter);
        }
        else
        {
            sum_2 += (number % ten_counter);
        }
        printf("%i",sum_1);
        printf("%i",sum_2);
    }
    


}
1

When you say number /= 10, you are actually reducing the number variable itself. So after counting and getting the value for count, the value of number is equal to zero

1
  • 1
    Oh thanks, man :D
    – KevinWGS
    Jun 20 '20 at 23:19

You must log in to answer this question.

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