by Dinesh Thakur Category: Control Structures

While performing a division operation, if the divisor is zero, the division by zero error occurs and program execution halts abruptly. The program segment given below calculates and prints the value of a/b without causing a division by zero error.

if (b == 0)

printf("Division by zero\n");

else {

printf ("%f", z);

The if-else statement tests a value of variable band if it is equal to 0, a message Division by zero is printed (and program execution continues); otherwise the value of alb is calculated and printed.

#include <stdio.h>

void main()

{

        int a,b;

        float z;

        clrscr();

        printf("Enter the Value of a :\n");

        scanf("%d",&a);

        printf("Enter the Value of b :\n");

        scanf("%d",&b);

        if (b == 0)

           printf("Division by zero\n");

           else {

                     z = a / b;

                     printf ("%f", z);

                  }

 

           getch();

}

     C Program division without causing a division by zero error



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article