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(); }