Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

#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);

}
like image 814
user244775 Avatar asked Mar 21 '10 01:03

user244775


People also ask

What happens if you divide by 0 in C?

Because what happens is that if we can say that zero, 5, or basically any number, then that means that that "c" is not unique. So, in this scenario the first part doesn't work. So, that means that this is going to be undefined. So zero divided by zero is undefined.

What type of error is division by zero in C?

Dividing a number by Zero is a mathematical error (not defined) and we can use exception handling to gracefully overcome such operations. If you write a code without using exception handling then the output of division by zero will be shown as infinity which cannot be further processed.

How do you get a zero from division?

This is just multiplying a number by its reciprocal. A Number Divided by 1 a1=a Just like multiplying by 1, dividing any number by 1 doesn't change the number at all. 0 Divided by a Number 0a=0 Dividing 0 by any number gives us a zero. Zero will never change when multiplying or dividing any number by it.

Is Dividing by 0 undefined behavior?

Division by zero has undefined behavior, and can result in crashes or incorrect program output.


1 Answers

#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);

}
like image 190
Carlos Avatar answered Sep 28 '22 04:09

Carlos