An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Operators are the foundation of any programming language. Thus the functionality of C/C++ programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands.
Arithmetic Operators
It includes basic arithmetic operations like addition, subtraction, multiplication, division, modulus operations, increment, and decrement.
The Arithmetic Operators in C and C++ include:
- + (Addition) – This operator is used to add two operands.
- – (Subtraction) – Subtract two operands.
- * (Multiplication) – Multiply two operands.
- / (Division) – Divide two operands and gives the quotient as the answer.
- % (Modulus operation) – Find the remains of two integers and gives the remainder after the division.
- ++ (Increment) – Used to increment an operand.
- — (Decrement) – Used to decrement an operand.
Example of Arithmetic Operators in C
#include<stdio.h>
int main()
{
int a=10, b=7;
printf("Welcome to DataFlair tutorials!\n\n");
printf("The Addition of %d and %d is: %d\n",a,b,a+b);
printf("The Subtraction of %d and %d is: %d\n",a,b,a-b);
printf("The Multiplication of %d and %d is: %d\n",a,b,a*b);
printf("The Division of %d and %d is: %d\n",a,b,a/b);
printf("The Modulus operation of %d and %d is: %d\n",a,b,a%b);
printf("The Incremented value ++a is: %d\n",++a);
printf("The Decremented value --b is: %d\n",--b);
return 0;
}
Example of Arithmetic Operators in C++
Here is a code in C++ which illustrates all the basic arithmetic operators:
#include <iostream>
using namespace std;
int main()
{
cout<<"Welcome to DataFlair tutorials!\n\n"<<endl<<endl;
int a = 10, b = 7;
cout<<"The Addition of "<< a << " and " << b << " are: " << a + b <<endl;
cout<<"The Subtraction of "<< a << " and " << b << " are: " << a - b <<endl;
cout<<"The Multiplication of "<< a << " and " << b << " are: " << a * b <<endl;
cout<<"The Division of "<< a << " and " << b << " are: " << a / b <<endl;
cout<<"The Modulus operation between "<< a << " and " << b << " is: " << a % b <<endl;
cout<<"The Incremented value ++a is: "<< ++a <<endl;
cout<<"The Decremented value --a is: "<< --a <<endl;
return 0;
}
Table for Arithmetic Operators in C and C++
Operator | Operand | Operation | Elucidation |
+ | a, b | a + b | Addition |
– | a, b | a – b | Subtraction |
* | a, b | a * b | Multiplication |
/ | a, b | a / b | Division |
% | a, b | a % b | Modulus operator – to find the remainder when two integral digits are divided |
++ | a | a ++ | Increment |
— | a | a – – | Decrement |
Comments
Post a Comment