Program for Division with if...else

Program for Division with if...else


#include<stdio.h>
void main()
{
float number,divisor,ans;
printf("Enter number to be checked and divisor: ");
scanf("%f%f",&number,&divisor);
if(divisor==0)
{
printf("Cannot Divide by Zero!!!");
}
else
{
ans=number/divisor;
printf("Answer of division is:%f",ans);
}
}

Explanation:
We know that division by zero is not possible and if we don't take care of this condition and user puts divisor as 0 then program may get shut abruptly(error will occur).
So, we take care of this condition using if...else conditions.

Note:
We have used float as data type so our format specifier is %f and not %d.

Output:





No comments:

Post a Comment