0

I am getting Floating Point Exception when i run this program on Linux.

#include<stdio.h>
#include<math.h>
int main()
{
    int num, num1, num2, n, i, j = 0, t, count = 0;
    int root;

    printf("Enter number of test cases\n");
    scanf("%d", &t);
    while (j < t)
    {
        scanf("%d", &num);
        root = (int)sqrt(num);
        for (i = 1; i < root; i++)
        {
            printf("Inside for");
            if (num % i == 0)
                num1 = i;
            while (num1 > 0)
            {
                n = num1 % 10;
                num1 = num1 / 10;
                if (n == 3 || n == 5 || n == 6)
                    count++;
            }
            if (num % num1 == 0)
            {
                num2 = (int)num / num1;
                while (num2 > 0)
                {
                    n = num2 % 10;
                    num2 = num2 / 10;
                    if (n == 3 || n == 5 || n == 6)
                        count++;
                }
            }
        }
        j++;
        count = 0;
        printf("%d", count);
    }
    return 0;
}

Can anyone please tell me how to correct it

5
  • You might want to indent your code, it will be much more easier to read then. Commented Oct 15, 2013 at 10:59
  • It would help if you could properly indent your code and maybe determine at which line the program crashes. Commented Oct 15, 2013 at 11:01
  • The only place you use floating point is the sqrt call. When the exception happen, what is the value of num? Commented Oct 15, 2013 at 11:01
  • 1
    @mbratch20 int will automatically be converted by the compiler. Commented Oct 15, 2013 at 11:02
  • 2
    In num % num1, if num1 is 0 (you exit the loop when it becomes zero, so that's more or less an expected case), you divide by zero. Linux is silly and calls that a floating point exception. Commented Oct 15, 2013 at 11:05

3 Answers 3

1
    while (num1 > 0)
    {
        n = num1 % 10;
        num1 = num1 / 10;
        if (n == 3 || n == 5 || n == 6)
            count++;
    }
    if (num % num1 == 0)

You rolling loop unless num1 becomes 0, then dividing by zero. Division by zero is guaranteed SIGFPE (at least on x86 and amd64). Despite name, it have nothing to do with floating point numbers.

Sign up to request clarification or add additional context in comments.

2 Comments

SIGFPE is a Unix/Posix signal, not an x86 or ARM requirement.
@EricPostpischil sure thing. Not sure if it is required to trigger when dividing by zero on other CPUs, tho - that's why remark. And there are many other things where SIGFPE could happen, but it results just a quiet NaN.
0

Using a debugger:

Starting program: /home/david/demo 
Enter number of test cases
2
10

Program received signal SIGFPE, Arithmetic exception.
0x000000000040076d in main () at demo.c:26
26              if (num % num1 == 0)
(gdb) p num1
$1 = 0
(gdb) 

As you can see, the value of num1 is 0, division by 0

Comments

0

Print the value of num1 before % operation, it gets 0 and then you exception of division by 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.