Example & Tutorial understanding programming in easy ways.

What is divide by zero error? how handle in C++ ?

Divide by zero error in C++:
Basically, sometime when we perform division operation then it may be possible that you divide some number by 0. so it is give an error and program terminate uncorrectly so this problem is called as divide by zero error i C++:

Without exception handling:

program:

#include< iostream>
int main()
{
int n=10,num;
cout<<"Enter number to divide 10"<< endl;
cin>>num;
n=n/num;
cout<< n;
}


output-

Enter number to divide 10
0
error

-In this program, when control gies on 'n=n/0', then program terminated or give run time error which is problem.

Solve this problem:

program:

#include< iostream>
int main()
{
int n=10,num;
cout<<"Enter number to divide 10"<< endl;
cin>>num;
try:
n=n/num;
cout<< n;
catch:
cout<<"Wrong operation!,It is divisible by zero";
}


output-

Enter number to divide 10
0
Wrong operation!,It is divisible by zero

-In this program, when error arives then program not close ,the control goes on catch block then it execute correctly.




Read More →