In c++ there are different possible solutions for dividing two number or variable in decimal or floating values. Also in embedded domain many time bit-wise right shift operator also used. So if you are finding answer of How to divide in c++ then this is perfect article for you to learning different solutions.
1. How to divide in c++ decimal
#include <iostream>
using namespace std;
int main()
{
cout<<"Division 100/10 : "<< 100/10 <<endl;
// Division 100/10 : 10
return 0;
}
Here we can directly divide two decimal numbers using division operator “/”. Decimal1/Decimal2 will give you decimal division number.
2. How to divide variables in c++
#include <iostream>
using namespace std;
int main()
{
int Value1= 100, Value2=10;
cout<<"Division Value1/Value2 : "<<Value1/Value2 <<endl;
return 0;
}
If you have two variables then you can assign values in variable and then divide using division operator. Here if both variables are decimal then output also will be in decimal. If any of one will be floating point then you will get result in floating value.
3. How to divide in c++ with floating result
#include <iostream>
using namespace std;
int main()
{
cout<<"Division : "<<1.0/6 <<endl;
// Division : 0.166667
return 0;
}
Here if you see for getting result in floating value you can add floating value for division. if you try 1/6 = 0 but if you try 1.0/6 = 0.166667 . By adding floating value result will be convert in floating digit.
4. How to divide in c++ using div() function
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
div_t divResult = div(100, 8);
//12 * 8 = 96 , 96 + 4 = 100
cout << "Quotient of 100/8 = " <<divResult.quot << endl;
//Quotient of 100/8 = 12
cout << "Remainder of 100/8 = "<<divResult.rem << endl;
//Remainder of 100/8 = 4
return 0;
}
You can use #include <cstdlib> for getting divisions using div(a,b) function which will give you result of a/b. divResult.quot will give you Quotient and divResult.rem will give you Remainder of division. Where divResult is type of div_t.
5. How to divide in c++ using bit-wise right shift operator ( without using division operator)
#include <iostream>
using namespace std;
int main()
{
// Using bitwise Right shift Divide by 2
cout<<"Division : "<<(100 >> 1);
//Division : 50
return 0;
}
Using right shift operator we can shift bit value to right so it will give us division by 2. Here 100 Dec = 01100100 If we right shift by 1 then 00110010 = 50 Dec. By checking binary values you can get information that using right shift operator lst 0 is shifted and new 0 is added at left side.
CONCLUSION for How to divide in c++
We have provided different 5 Methods to divide values in c++. You can use any of methods based on your requirement in program. Generally for decimal value we use direct division operator. But sometimes in embedded software development they prefer to use right shift operator for making it more speedy. If you have any further doubts please share your comment. For more c++ related articles please visit to out website.
Leave a Reply