0

I can pretty much select the digits, multiply the digits by 2 & add the particular digits of the multiplied number. But as of now I can only print them not add them. I also have to add this sum to the remaining numbers in the credit card & then check for modulo 10. How should I do this? Should I change my code(it took me a serious effort to get to this point)?

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

int main(void)

{
    long long n = get_long("n:"); // gets you CC number
    int count = 0;
    int z;
    int k;
    int a;
    int b;
    int c;
    int d;
    
    
    while(n / 10 >= 1) 
    {
        z = n % 10;    
        count++;
        if(count % 2 == 0)  // selects the digit in even places
        {
            k = 2*z;     // multiplies the digit in even places
            a = k % 10;  // gives 1's place of k
            b = k / 10;  // gives 10's place of k
            c = a + b;   // adds the digit
            printf("%i\n", c); // this just exists to check whether the above process is happening
        }
        
        n = n / 10;
        
    }
    
    

    if(n / 10 == 0) // This helps to find the rightmost digit
    {
        z = n % 10;
        count = count + 1;
        if(count % 2 == 0)
        {
            k = 2*z;
            a = k % 10;
            b = k / 10;
            c = a + b;
        }
        
       
     }
    
    

}

1 Answer 1

0

You have declared variables to work out the result of your digit math. Why not just declare another variable and add the result to it each time?

You must log in to answer this question.

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