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...

## All 6 Replies

can you send code too see it

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;
}``````

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.