Arithmetic Operators in C++

Arithmetic Operators in C++ with Examples:

In this article, I am going to discuss Arithmetic Operators in C++ with Examples. Please read our previous article where we discussed Variables in C++ with Examples. At the end of this article, you will learn operators, Arithmetic Operators in C++ with Examples, and also expressions just to get the basic idea. So let us understand what are operators.

Operators in C++ Language

Operators are predefined in C++ and it is used to perform a computational task. Usually, operators are symbols. The C++ built-in operators are as follows.

Operators in C++ Language

They are useful for performing some operations and there are symbols used for operators like in mathematics i.e. ‘+’ symbol used for adding two numbers. Every symbol is having some operation associated with that. So let us look at some of the operators that we have written above.

1st is arithmetic operators: Arithmetic operators are addition, subtraction, multiplication, division, and the modulus. These are the operators we will discuss in this article.

Then the other operators is relational operators, these are useful for comparing numbers, comparing values. So, these operators are less than, less than equal to, greater than, greater than equal to, equal to, and not equal to. These operators will learn in our upcoming articles.

Next is logical operators. These are AND, OR, NOT logical operations. Bitwise operations are also similar but they have a big difference compared to these two. These are also AND, OR, NOT & XOR. We will learn about this in separate articles.

The increment and decrement operators are ++ and –. These are more commonly used operations in C++ programming. We will learn about these operators in our upcoming articles.

Next is the assignment operator, we have used this one for storing the value, and there are many other operations so. We will learn them as we go on covering the new topics in upcoming articles. Now we will learn about arithmetic operations and also, we will learn how to form expressions.

Arithmetic Operators in C++

The operators that are used to perform mathematical operations are called Arithmetic operators. Symbols are similar to the ones which we used in mathematics.

  1. The addition of number is done using ‘+’
  2. Subtraction ‘-‘
  3. Multiplication ‘*’ (asterisk) (note: In mathematics we use x but in C++ it is an asterisk.)
  4. Division ‘/’
  5. Modulus ‘%’ (to identify the remainder or to find modulo)
  6. Increment Operator ++ (adds one to an already existing value or variable)
  7. Decrement Operator — (subtract one from an already existing value)

Note: Increment or decrement operators are usually used in loops, which will be discussed later.

How do Arithmetic Operators work in C++?

Let us understand how they work. They are meant for performing operations on data. First of all, you should have data, where do you keep data in your program? In the variables i.e.
int x, y, z;
We have 3 variables x, y, and z.
x = 15;
y = 3;
Now x is having a value of 15 and the variable y is having a value of 3. Now let us perform those operations.

z = x + y; Here, z assign x + y then the value of x and y are added and the result is stored in the z variable. So, the 15 + 3 = 18 is stored in z variable.

z = x – y; Now if z assign x – y then this is 15 – 3 = 12 and this 12 is stored in z. Then let us look at next.

z = x * y; If we write z assign a * b then this is 15 * 3 that is the 45 is stored in the z variable. Now other 2 we have to see. These are important. There is a divide and mod.

z = x / y; So, if we write z assign x / y and the result is in z so what does it mean. x is divided by the variable y value, so the calculated value is 15 / 3 is 5. So, 5 is stored in z. Here 15 is the dividend, 3 is the divisor and 5 is the quotient. And we know after division there is no remainder so, the remainder is 0. Here we have stored the quotient using the ‘/’ operator. What if we want to store the remainder? So, for storing the remainder we will use the ‘%’ operator.

z = x % y; Here we write z assign x % y so 15 % 3, as the remainder is 0 so 0 will be stored in z. If we change values of x and y then,
x = 10;
y = 7;
Now the 10 % 7 will store in z, so the result is 10 % 7 = 3. So, 3 will store in z.

Here in all operations, we have taken integer so it will be integer only. So, this is the one important thing that we should know if you’re performing division on integers. Then the result is also an integer. Even though if you’re getting the floating point then it will not be showing it. How to get floating-point results? We will see this later.

So, there’s a difference we should know clearly that divide gets quotient and mod gives remainder. It’s sometimes confusing for the students to understand this but this is very easy. So, you should know clearly what is quotient and what is the remainder. Performing division on integers we won’t get it but we have to typecast it to get float.
z = (float) x / y;

What is Type Casting in C++?

So, we can change the data type of this result. This is true if you write about it we want it to be float. This should be float. So, this is called typecasting, and typecasting means changing the data type. Here the quotient will be in decimal. The result would be an integer but we are converting it into the float at the time of division. So, this is how we can get the float result from the integer division. Now let us see how to get the floating-point result if two integers are divided. For this, we have to take float data type:

float x = 13.5f, y = 4.1f, z;

So, we want the result in float.

z = x / y;

Now we have three variables all out are float only, now if I want to divide x by y then the result will be floating point result only, and the result we will take it in variable z so I don’t have to worry about typecasting anything because x and y are off type float so the result will be float.

So, from this, we can understand that if we have integers and you perform any arithmetic operation on the integer, we get the result in an integer. If it is a float then we get the result in float. Similarly, if you have any other data type then you would get the same data type. Now next we will show one more thing. We have seen the mod operator ‘%’ which gives the remainder when the two no. are divided.

So, this mod operation can be performed over integers. Can we do x % y here and store the result in some variable? No, we cannot perform the mod operation on float no. this you must know. Then I have one more data type, here that is a character type.

char x = 13, y = 5, z;

z = x / y;

Suppose we want to perform x % y and store the result in c. Is it allowed on characters? Yes, it is allowed. So the mod is allowed only on integers and characters.

Here you may get confused that how we are storing numbers in character so if you remember we have learned in the data types that characters are nothing but numbers. These are codes.

So, every character is having an ASCII code. Actually, a character is an integral type only. It stores the codes of the character. So, if you’re not storing any character directly you can assign a code. So the mod operation is allowed on these two.

Arithmetic Operators Example in C++

Let us write a program, which uses all the arithmetic operators.

#include <iostream>
using namespace std;
int main ()
{
    int no1, no2;
    cout << "enter the values for number 1 and number2 \n";
    cin >> no1 >> no2;//to read the value of two number
    cout << "Addtion of two numbers:\t" << no1 + no2 << endl;
    cout << "Subtraction of two numbers:\t" << no1 - no2 << endl;
    cout << "Multiplication of two numbers:\t" << no1 * no2 << endl;
    
    //note we should no pass no2 value as zero.input validation needs to be done 
    //but will discuss in later section after discussion conditional statements
    cout << "Division of two numbers:\t" << no1 / no2 << endl;
    cout << "Modulus of two numbers:\t" << no1 % no2 << endl;
    
    //note there is preincrement/decrement and post increment/decrement 
    //which will be discuss later in this section
    cout << "Increment no1:\t" << ++no1 << endl;
    cout << "Decrement  no1:\t" << --no1 << endl; 
    return 0;
}
Output:

Arithmetic Operations in C++ with Examples

In the next article, I am going to discuss Operator Precedence and Expressions in C++ with Examples. Here, in this article, I try to explain Arithmetic Operations in C++ with Examples and I hope you enjoy this Arithmetic Operations in C++ with Examples article.

Leave a Reply

Your email address will not be published.