-2

Here is a simple program which counts number of Individual digits that exactly divides given number.

#include<stdio.h>

int main()
{
    long long t, val, temp, count = 0, val1;
    scanf("%lld", &t);
    while (t--)
    {
        count = 0;
        scanf("%lld", &val);
        val1=val;
        while(val1 != 0)
        {
            temp = val1 % 10;
            val1 = val1 / 10.0 ;
            if (val % temp == 0)
            {
                count++;
            }
        }
        printf("%lld", count);
    }
}

It throws divide by zero exception when my input is

1
300

meaning number of test cases is 1 and value is 300. But here I am dividing it by a constant 10 so why is that throwing that exception. So what should I do now to handle here? *I even tried in cpp and faced same problem

chrk
  • 4,037
  • 2
  • 39
  • 47
Abhinav Konda
  • 225
  • 1
  • 11

1 Answers1

6
val%temp

if temp is 0 (which seems to be the case in your program), you are invoking undefined behavior.

ouah
  • 142,963
  • 15
  • 272
  • 331