Operator and Type of Operators with the example Content in C ,C++

Table of Contents

Operator

In programming, an operator is a symbol or a function that performs an operation on one or more operands to produce a result. Operators are used to manipulate data and perform various computationshttps://www.highrevenuegate.com/q93su35u?key=c36e639009b94e3538649a1c01c3d9c8

An operator in programming is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational, or logical operation and produce a final result. Operators are used to manipulate data, perform calculations, and make decisions in programs.

Type of operator

Here are some common types of operators found in programming languages:

  1. Arithmetic Operators: Used for mathematical operations like addition, subtraction, multiplication, division, modulus, and exponentiation. Examples include + (addition), – (subtraction), * (multiplication), / (division), % (modulus), and ** (exponentiation).
  2. Assignment Operators: Used to assign values to variables. The most basic assignment operator is the equals sign (=), but there are also compound assignment operators like +=, -=, *=, and /=, which combine assignment with arithmetic operations.
  3. Comparison Operators: Used to compare two values and return a Boolean result (true or false). Examples include == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).
  4. Logical Operators: Used to combine or negate logical expressions. The common logical operators are && (logical AND), || (logical OR), and ! (logical NOT).
  5. Bitwise Operators: Used to perform operations on individual bits of binary numbers. Examples include &, |, ^ (bitwise AND, OR, XOR), << (left shift), >> (right shift), and ~ (bitwise complement).
  6. Unary Operators: Operate on a single operand. Examples include – (negation), ++ (increment), — (decrement), and ! (logical NOT).
  7. Ternary Operator: A conditional operator that takes three operands and returns a value based on a condition. The syntax is typically “condition ? value1 : value2”. If the condition is true, value1 is returned; otherwise, value2 is returned.

Here are examples of arithmetic operators using C, C++, and Java

Addition

C

// C
#include <stdio.h>

int main() {
    int a = 5;
    int b = 3;
    int result = a + b;
    printf("Result: %d\n", result);
    return 0;
}

C++

// C++
#include <iostream>

int main() {
    int a = 5;
    int b = 3;
    int result = a + b;
    std::cout << "Result: " << result << std::endl;
    return 0;
}

Java

// Java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;
        int result = a + b;
        System.out.println("Result: " + result);
    }
}

Subtraction (-):

C

// C
#include <stdio.h>

int main() {
    int a = 5;
    int b = 3;
    int result = a - b;
    printf("Result: %d\n", result);
    return 0;
}

C++

// C++
#include <iostream>

int main() {
    int a = 5;
    int b = 3;
    int result = a - b;
    std::cout << "Result: " << result << std::endl;
    return 0;
}

Java

// Java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;
        int result = a - b;
        System.out.println("Result: " + result);
    }
}

Multiplication (*):

C

// C
#include <stdio.h>

int main() {
    int a = 5;
    int b = 3;
    int result = a * b;
    printf("Result: %d\n", result);
    return 0;
}

C++

// C++
#include <iostream>

int main() {
    int a = 5;
    int b = 3;
    int result = a * b;
    std::cout << "Result: " << result << std::endl;
    return 0;
}

Java

// Java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;
        int result = a * b;
        System.out.println("Result: " + result);
    }
}

Division (/):

C

// C
#include <stdio.h>

int main() {
    int a = 5;
    int b = 3;
    int result = a / b;
    printf("Result: %d\n", result);
    return 0;
}

C++

// C++
#include <iostream>

int main() {
    int a = 5;
    int b = 3;
    int result = a / b;
    std::cout << "Result: " << result << std::endl;
    return 0;
}

Java

// Java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;
        int result = a / b;
        System.out.println("Result: " + result);
    }
}

Modulus (%)

C

// C
#include <stdio.h>

int main() {
    int a = 5;
    int b = 3;
    int result = a % b;
    printf("Result: %d\n", result);
    return 0;
}

C++

// C++
#include <iostream>

int main() {
    int a = 5;
    int b = 3;
    int result = a % b;
    std::cout << "Result: " << result << std::endl;
    return 0;
}

Java

// Java
public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 3;
        int result = a % b;
        System.out.println("Result: " + result);
    }
}

These examples demonstrate the usage of arithmetic operators in C, C++, and Java to perform addition, subtraction, multiplication, division, and modulus operations on given operands.

Scroll to Top