| by Arround The Web | No comments

Integer Division in C++

Today, we use one of the datatypes of C++ programming language which is the int datatype, also  known as integers. Through this, we will do the division operation in C++ language. As we know, the division is one of the basic methods of mathematics. We do division operations in mathematics so that we can partition a large group into smaller subgroups so that each group can get the same amount of items. In this tutorial, let us just explore more in-depth mathematical technique called division and see how we implement it in C++ programming language.

Integer division is the method where we take the input number as an integer dividend and the divisor can be an integer or of any datatype input number. Through this, we get the quotient which is the returned value of the division operation. We also get a reminder which is the left-out value in the division method.

Syntax:

To understand the concept of integer division, let’s understand the syntax and the implementation style in C++ programming language. In division, we take two input variables to implement the division method. The first variable is the dividend which is “var1” and the second is the divisor which is “var2”. To perform the division operation, we use the forward slash “/” between the “var1” and “var2” variables.  Then, we store this method in another new variable which is named “division” in the syntax.

Example 1: Integer Dividend and Integer Divisor

Let us just begin by putting the integer division method’s very first and most basic example into practice. Before we start creating the integer division program that we desire to implement in the C++ programming language, we usually need a translator that allows us to write and execute the program. You can therefore install any C++ translator or use the online translator to create and run the code, provided it is suitable with the packages that are related to the implemented method which you wish to utilize in the program.

Now that the C++ translator is launched, you can begin implementing the code.  The header files must always be included in C++ projects to easily call the methods that we want to use throughout the C++ program. Since the header files are already incorporated into the C++ programming language, integrating them requires just one line of code. The initial and the only module that we normally add in an integer division C++ program is the “iostream” module which is used to showcase the data and take an input from the user. Furthermore, we use the “using namespace std” command to stop the objects, methods, and arguments from constantly referencing the same context throughout the rest of the program.

#include <iostream>
using namespace std;
int main()
{
    int var1, var2, div;
    cout<<"Enter value of var1: ";
    cin >> var1;
    cout<<"Enter value of var2: ";
    cin >> var2;
    div= var1/var2;
    cout<< endl <<"The return value is: "<<div;
    return 0;
}

 
The program’s main() function is then initiated. That is where we put the main code that we want to use into action. In line 5, we declare three variables of integer datatypes named “var1”, “var2”, and “div”. We use the cout() method to print the message on the user’s screen in quotation marks. To get the input value in “var1”, we use the predefined method of C++ language which is the cin() method. Then, we write a cout() method again to print another message which is related to the “var2” variable and we store the input value in it by getting the value using the cin() method again. In line 10, the “var1” is divided by “var2” and the return value is assigned to the “div” variable. To display the division result on the user’s screen, we use the cout() method and pass the “div” variable in it. At the end of the main() function, we return 0 to the main() function so the execution of the program is stopped and it will showcase the output on the user’s screen.

As you can see in the following illustration in the black screen, the user enters the “var1” value with “23” and “var2” value with “13”. After doing the division method, we get “1”. But basically, we have the result as “1.77” when calculating on the calculator. We get “1” because the data type of the “div” variable is “int”, so it ignores the value after the point.

Example 2: Integer Dividend and Float Divisor

Now, let’s do another example of integer division in the C++ programming language. We take an integer type value as a dividend and a floating-point number as a divisor. Let’s see what we get in return.

In a C++ program, we first include the header file in the program so that we can easily access the predefined functions. We include the “#include<iostream” library so that we can get the data from the user and print it to the user on the user’s console window. Then, we write a “using namespace std” directive so that we can’t share the same scope in the whole program.

#include <iostream>
using namespace std;

int main()
{
    int dividend = 645;
    float divisor = 24.7;
   
    cout << "The value of dividend is: " << dividend << endl;
    cout << "The value of divisor is: " << divisor << endl;

    cout << "\nThe result of division method is: " << dividend / divisor;
   
    int result = dividend / divisor;
    cout << "\nThe result of integer division method is: " << result;
   
    return 0;
}

 
In the main() function, we declare two variables on integer type dividend and float type divisor and initialize the values to both variables. In line 12, we print the division by calling the cout() method. Then, we declare an integer-type variable result, store the division in it, and print it.

The following is the output which is illustrated in the previously-implemented example. As you see, we first print the dividend value. Then, we print the divisor value. In the next line, we print the result of the division method which we performed on 26.1134. But the result shows a decimal value. To display the result in integer type, we declare a variable result and set the type of this variable as integer. Then, we print it. Now, the result is 26.

Conclusion

In this article, we learned what division is in C++ programming language and how it works in C++ programming language. We learned the implementation style of integer division. Then, we also implemented some examples of integer division to understand clearly the concepts of integer division working in C++.

Share Button

Source: linuxhint.com

Leave a Reply