how to check if there is a division by zero in c

user244775 picture user244775 · Mar 21, 2010 · Viewed 12.3k times · Source
#include<stdio.h>
void function(int);

int main()
{
     int x;

     printf("Enter x:");
     scanf("%d", &x);

function(x);

return 0;
}

void function(int x)
{
    float fx;

    fx=10/x;

    if(10 is divided by zero)// I dont know what to put here please help
        printf("division by zero is not allowed");
    else
        printf("f(x) is: %.5f",fx);

}

Answer

Carlos picture Carlos · Mar 21, 2010
#include<stdio.h>
void function(int);

int main()
{
     int x;

     printf("Enter x:");
     scanf("%d", &x);

function(x);

return 0;
}

void function(int x)
{
    float fx;

    if(x==0) // Simple!
        printf("division by zero is not allowed");
    else
        fx=10/x;            
        printf("f(x) is: %.5f",fx);

}