In a program, while compiling the compiler does not give an error but while executing it, i get an error.Only
"Floating point exception"
is printed on the output screen.Even a cout statement given at the start of the main function doesn't get executed.
Can anyone help me out with this?

Thanking you in advance...

It's not a syntax issue, since it compile; it is a runtime issue in which you allow your code to do something it is not supposed to do. In particular it happens to indicate that the issue involves something you may have attempted to do involving floating point.

Without posting code, finding your bug is mere guesswork. Something like this may arise from trying to take the square root of a negative number, or attempting to divide by zero as examples.

#include<stdio.h>
int ms(int n)
{
    int i,k=1;
    for(i=0;i<n;i=i+1){
        k=k*i;
    }
    return k;
}
int ncr(int n, int r)
{
    int i,j,k,l;
    i=ms(n);
    j=ms(r);
    k=ms(n-r);
    l=j*k;
    l=i/l;
    return l;
}
int main()
{
    int h;
    h=ncr(4,2);
    printf("%d",h);
    return 0;
}

Edited 5 Years Ago by Narue: added code tags

i'm getting output as "floating point exception" for the above code
why it is so...........

Edited 5 Years Ago by Naveen_: n/a

In function ms:

Initialize i to 1 in your for loop.

The way you have it setup now, you're returning zero for every value in your ncr function.

By doing this, you end up dividing by zero (when you divide by l), which is probably why you're getting the floating point exception.

Code should look like this:

#include<stdio.h>
int ms(int n)
{
    int i,k=1;
    for(i=1;i<n;i++){
        k*=i;
    }
    return k;
}
int ncr(int n, int r)
{
    int i,j,k,l;
    i=ms(n);
    j=ms(r);
    k=ms(n-r);
    l=j*k;
    l=i/l;
    return l;
}
int main()
{
    int h;
    h=ncr(4,2);
    printf("%d",h);
    return 0;
}

And it will return the value 6, which is correct.

Edited 5 Years Ago by nightcrew: Silly capitalization error

This article has been dead for over six months. Start a new discussion instead.