• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

Mr.CodeHunter

Programming and Code Solutions

  • Home
  • C++
  • About Us
  • Contact Us
  • Privacy Policy

How to divide in c++ ?

May 29, 2021 by Admin Leave a Comment

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.

Filed Under: All C++, C++

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search here

Social Media

  • Facebook
  • YouTube

SEE MORE

Fibonacci sequence c++

Fibonacci Sequence c++ is a number sequence which created by sum of previous two numbers. First two number of series are 0 and 1. And then using these two number Fibonacci series is create like 0, 1, (0+1)=1, (1+1)=2, (2+1)=3, (2+3)=5 …etc Displaying Fibonacci Series in C++ ( without recursion) Output: From given output we […]

map c++

C++ Map [Learn by Example]

C++ map is part of Standard Template Library (STL). It is type of Associative container. Map in c++ is used to store unique key and it’s value in data structure. But if you want to store non-unique key value then you can use Multi Map in c++. Let us first understand in detail what is […]

how to copy paste in turbo c++ ?

There are many different C++ IDE are available but still many students are using Turbo c++ for learning c/c++ programming languages. During using Turbo c++ if you are beginner you will be confuse for how to copy and paste in turbo c++ or if you have already copy some content and you want to paste […]

C++

return 0 c++

There are two different scenario return statement is used inside c++ programming. We can use return 0 c++ inside main() function or other user defined functions also. But both have different meanings. return 0 c++ used inside Main function return 0 c++ used inside other user defined function What is meaning of return 0 and […]

C++

c++ expected a declaration [ SOLVED]

When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code. Main reasons for errors are: Incorrect use/declaration inside namespace Statements are added out of scope Required statement need to add inside main/function Solution-1 | Expected a […]

C++

c++ cannot open source file “errno.h” [SOLVED]

Normally you will face c++ cannot open source file “errno.h” in MS Visual Studio c++ projects. These are some solutions to remove opening errors for “errno.h” file. I got the errors to go away by installing the Windows Universal CRT SDK component, which adds support for legacy Windows SDKs. You can install this using the […]

Footer

DISCLAIMER

The information contained on https://www.mrcodehunter.com is for general information purposes only. We assumes no responsibility for errors or omissions in the contents on the Service.

SITEMAP XML

Sitemap

Recent

  • Fibonacci sequence c++
  • C++ Map [Learn by Example]
  • how to copy paste in turbo c++ ?
  • return 0 c++
  • c++ expected a declaration [ SOLVED]

Search

Copyright © 2025 ยท Mr Code Hunter