-4

If it fits the algorithm, it should have an output of true and if it doesn't, the output should be false. Any idea where it goes wrong?

I tried

1586455534096 ; output : false(fail)

49927398716 ; output : true (ok)

984697300577 ; output : false (fail)

Code

#include<stdio.h>
#include<string.h>

int main()
{
  char str[100];
  int n,num[100],prod[100],remainder,sum,total;
  scanf("%s", str);
  for(n=0;n<100;n++)
    {
      if(str[n]==0) break;
      num[n] = str[n] - '0';
      if(n%2!=0)
        {
          prod[n]=num[n]*2;
          sum=0;
          while(prod[n]>=10)
            {
              remainder=prod[n]%10;
              sum=sum+remainder;
              prod[n]=prod[n]/10;
            }
          num[n]=sum;
          }
      total = total + num[n];
      }
 if(total%10==0)
    printf("true\n");
  else
    printf("false\n");
  return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
M.H.F
  • 81
  • 2
  • 5

3 Answers3

3

Your code working.

#include<stdio.h>
#include<string.h>

int main()
{
  char str[100];
  int n,num[100],prod[100],remainder,sum,total=0;
  scanf("%s", str);
  for(n=0;n<100;n++)
    {
      if(str[n]==0) break;
      num[n] = str[n] - '0';
      if(n%2!=0)
        {
          prod[n]=num[n]*2;
          sum=0;
          while(prod[n]>0)
            {
              remainder=prod[n]%10;
              sum=sum+remainder;
              prod[n]=prod[n]/10;
            }
          num[n]=sum;
          }
      total = total + num[n];
      }
 if(total%10==0)
    printf("true\n");
  else
    printf("false\n");
  return 0;
}

Into your code there are 2 main problems:

  1. no init value for total var at least
  2. the inner while have to use >0 as condition.

My correction on your code

#include<stdio.h>
#include<stdint.h>
#include<string.h>

int main()
{
    char format[32];
    char str[100]={0};
    uint32_t n=0;
    uint32_t num = 0;
    uint32_t total=0;

    snprintf(format, sizeof(format), "%%%zus", (sizeof(str)/sizeof(str[0]))-1 );

    scanf(format, str);

    while (str[n] != '\0')
    {
        num = str[n] - '0';

        if(n%2!=0)
        {
            num*=2;
            while(num>0)
            {
                total += num%10;
                num/=10;
            }
        }
        else
        {
            total += num;
        }

        n++;
    }

    if(total%10==0)
        printf("true\n");
    else
        printf("false\n");

    return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
0

At first glance total is uninitialized and having uninitialized values in the code leads to indeterminate values

Gopi
  • 19,784
  • 4
  • 24
  • 36
0

You never initialize total before you use it, which means its value is indeterminate and giving you undefined behavior. Might be the same with other variables.

Initialize the variables on declaration, like e.g.

int n, num[100] = { 0 }, prod[100] = { 0 }, remainder = 0, sum = 0,total = 0;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Sorry, but I don't quite understand. Do I put total=0 before any calculations? – M.H.F May 06 '16 at 06:55
  • Tried, but still failed. – M.H.F May 06 '16 at 07:01
  • 1
    @M.H.F Then you need to use a debugger to step through the code, line by line, while monitoring variables and values to see when and where it goes wrong. Start with some input that you *know* the values in each step, so you can easily verify. – Some programmer dude May 06 '16 at 07:15