0

i'm currently trying to do credit.c in pset 1, and I've managed to get every other digit separated and multiplied by 2, then added together, the problem is the next step "Now let’s add those products’ digits (i.e., not the products themselves) together", i can't figure out how to separate a potential double digit number without messing the rest of the single digits up, here is my code:

//program to determine if a credit card is valid or not.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
{
long long n = get_long_long ("Please enter an number:\n");
int d = 0;
int s = 0;
int f = 0;

n = n * 10; //This is to add one number onto the n value so that the program will take every other number as its supposed to 

while(n > 0) //terminate loop when n reaches zero
{


        d = n % 10; //takes the last number off and assigns it to digit variable

        d = (d * 2);

        s = d + s;

        printf("%i\n", d); //should print the digit one after another 



        n = n  / 100;}
         //divides original number by 100

printf("sum = %i", s);

    return 0;
}



}

output using the example of davids credit card is 0 2 0 0 0 0 12 0 8 sum = 22

but i need it to be

0 2 0 0 0 0 1 2 0 8 sum = 13

any help would be very appreciated

0

Check if a digit is a double digit. Then use the method of separating digits from a card to separate the digits and add them.

If this helped, click on the tick mark.

You must log in to answer this question.

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