0

With this code, I want to "Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together"get the ". I don't get the correct results, however. (i.e., 378282246310005 should give 27 but my code gives 738)


char name[20];
char dist[3];
long long number = GetLongLong();

sprintf(name, "%lld", number );

int length = strlen(name);


int SUM1 = 0;
if ( (length == 13) || (length == 15) )
{
    for (int a = 1; a < length-1; a+=2)
    {
        int prodig = name[a] * 2;
        sprintf(dist, "%d", prodig);
        if (strlen(dist) == 2)
        {
            SUM1 = SUM1 + dist[0] + dist[1];
        }
        else
        {
            SUM1 += prodig;
        }
    }

}

if (length == 16)
{
    for (int a = 1; a < length + 1; a+=2)
    {
        int prodig = name[a] * 2;
        sprintf(dist, "%d", prodig);
        if (strlen(dist) == 2)
        {
            SUM1 = SUM1 + dist[0] + dist[1];
        }
        else
        {
            SUM1 += prodig;
        }
    }
}

printf("%d \n", SUM1);
2
  • with what code?
    – Cliff B
    Dec 24 '16 at 23:08
  • Sorry. I edited it. Dec 24 '16 at 23:16

You must log in to answer this question.

Browse other questions tagged .