Min menu

Pages

VB

Factors in C ++

 

factors in C++

The concept of factors in  C++

factors operators ) They are symbols that have a specific meaning, and we can divide the factors into  5  basic groups as follows:

  • Arithmetic Operators

  • Comparison Operators

  • Logical Operators

  • Bitwise Operators

  • Assignment Operators


There are also other factors, which we will see at the end of the lesson.

What factors are used in arithmetic operations? (Arithmetic Operatorsin C++

the name of the employeehis codeExampleExplanation of the code
Assignment=a = bI give  a a value b
Addition+a + bAdd value  b to value a
Subtraction-a - bSubtract a value  b from a value a
Unary plus++aMultiply the value  a by the factor +
Unary minus--aMultiply the value  a by the factor -
Multiplication*a * bmultiply a value  a by b
Division/a / bDivide a value  a by a value b
Modulo%a % bTo get the last digit that remains when we divide a value  a by a value b
Increment++a++To add  1  to a value  a and use it in loops
Decrement--a--To decrement  1  from a value  a and use in loops


Factors that are used in arithmetic operations in C++

worker  = (Assignment Operator) in C++

The operator  = is used to assign a value to a variable.

Example

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 5; // 5 the value a given the variable
		  int b = a; // a the value of the variable b we give the variable
	  
		  cout << "a = " << a << endl;
		  cout << "b = " << b;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    a = 5
		  b = 5
	


worker  + (Addition Operator) in C++

The operator  + is used to add a value to a value, ie in addition operations.

Example

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 3;
		  int b = 4;
	  
		  int c = a + b; // c = 3 + 4 = 7
	  
		  cout << "c = " << c;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    c = 7
		


worker  - (Subtraction Operator) in C++

The operator  - is used to decrement a value from a value, ie in subtraction operations.

Example

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 3;
		  int b = 4;
	  
		  int c = a - b; // c = 3 - 4 = -1
	  
		  cout << "c = " << c;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    c = -1
		


worker  + (Unary-Plus Operator) in C++

It means multiplying the value by the factor  +.

Example

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  // b in the variable Unary-Plus has a value greater than zero, then we put the value of a here and we put it in the variable
		  int a = 10;
		  int b = +a; // b = +(10) = 10
	  
		  cout << "b = " << b << endl;
	  
		  // b in the variable Unary-Plus has a value less than zero, then we put the value of a here and we put it in the variable
		  a = -10;
		  b = +a; // b = +(-10) = -10
	  
		  cout << "b = " << b;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    b = 10
		  b = -10
	


worker  - (Unary-Minus Operator) in C++

It means multiplying the value by the factor  -.

Example of multiplying the value by the factor 

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  // b in the variable Unary-Minus has a value greater than zero, then we put the value of a here and we put it in the variable
		  int a = 10;
		  int b = -a; // b = -(10) = -10
	  
		  cout << "b = " << b << endl;
	  
		  // b in the variable Unary-Minus has a value less than zero, then we put the value of a here and we put it in the variable
		  a = -10;
		  b = -a; // b = -(-10) = 10
	  
		  cout << "b = " << b;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    b = -10
		  b = 10
	


worker  * (Multiplication Operator) in C++

The operator  * is used to multiply a value by a value, ie in multiplication operations.

Example of multiplying a value by

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 6;
		  int b = 5;
	  
		  int c = a * b; // c = 6 * 5 = 30
	  
		  cout << "c = " << c;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    c = 30
		


worker  / (Division Operator) in C++

The operator  / is used to divide a value by a value, ie in division operations.

Example of dividing a value by a value

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 8;
		  int b = 5;
	  
		  int c = a / b; // c = 8 / 5 = 1
	  
		  cout << "c = " << c;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    c = 1
		

Note:  The reason why no numbers appear after the comma is that we defined the variables as integers  int.



worker  % (Modulo Operator) in C++

The worker  % is called Modulo And it's called Reminder In mathematics, it is the last number remaining in the division process.
So we use the Modulo To get the last remaining number of division.
And it has many benefits, for example, we can use it to find out whether a number is single or double ( Which Even or Odd ) And this we explained in detail in the article Algorithms.

In this example, we will store the number that remains from the division in the variable  c.

An example of storing the number that remains from division in the variable  c.

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 8;
		  int b = 5;
	  
		  int c = a % b; // c = 8 % 5 = 3
	  
		  cout << "c = " << c;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    c = 3
		


worker  ++ (Increment Operator) in C++

The operator  ++ is used to increment the value of a variable by one, and this method is often used in loops to increase the value of the counter by one per cycle with less code.

Example of increasing the value of a variable by one

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 5;
	  
		  a++; // a = 5 + 1 = 6
	  
		  cout << "a = " << a;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    a = 6
		


worker  -- (Decrement Operator) in C++

The operator  -- is used to decrement the value of a variable by one, and this method is often used in loops to decrement the value of the counter by one per cycle with less code.

Example of decreasing the value of a variable by one

Main.cpp
                    #include <iostream>
		  using namespace std;
	  
		  int main()
		  {
		  int a = 5;
	  
		  a--; // a = 5 - 1 = 4
	  
		  cout << "a = " << a;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    a = 4