C++ Operators Practice Questions with Solutions

Operators are one of the most fundamental concepts in C++ programming. They allow you to perform calculations, compare values, assign data, and make logical decisions within a program. Almost every C++ application, from simple calculators to complex software systems, relies heavily on operators.

C++ provides several categories of operators, including arithmetic, relational, logical, assignment, increment/decrement, and conditional operators. Understanding how these operators work is essential before moving on to conditional statements, loops, functions, and object-oriented programming. C++ Operators practice questions with solutions help to understand the concepts.

In this chapter, you’ll solve practical operator-based programming questions that strengthen your understanding of expressions, calculations, comparisons, and logical operations. Each question includes a problem statement, complete C++ solution, sample input/output, explanation, and concepts covered.


1. C++ Program to Perform Basic Arithmetic Operations

Problem Statement

Write a C++ program to perform addition, subtraction, multiplication, division, and modulus on two integers entered by the user.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int num1, num2;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    cout << "Addition = " << num1 + num2 << endl;
    cout << "Subtraction = " << num1 - num2 << endl;
    cout << "Multiplication = " << num1 * num2 << endl;
    cout << "Division = " << num1 / num2 << endl;
    cout << "Modulus = " << num1 % num2 << endl;

    return 0;
}

Sample Input

Enter first number: 20
Enter second number: 5

Sample Output

Addition = 25
Subtraction = 15
Multiplication = 100
Division = 4
Modulus = 0

Explanation

The program demonstrates all five basic arithmetic operators available for integer values.

Concepts Covered

  • Arithmetic Operators
  • Integer Division
  • Modulus Operator
  • User Input

2. C++ Program to Find the Quotient and Remainder

Problem Statement

Write a C++ program to calculate the quotient and remainder after dividing two integers.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int dividend, divisor;

    cout << "Enter dividend: ";
    cin >> dividend;

    cout << "Enter divisor: ";
    cin >> divisor;

    cout << "Quotient = "
         << dividend / divisor << endl;

    cout << "Remainder = "
         << dividend % divisor;

    return 0;
}

Sample Input

Enter dividend: 27
Enter divisor: 4

Sample Output

Quotient = 6
Remainder = 3

Explanation

The division operator (/) returns the quotient, while the modulus operator (%) returns the remainder.

Concepts Covered

  • Division Operator
  • Modulus Operator
  • Integer Arithmetic

3. C++ Program to Check Whether One Number is Greater Than Another

Problem Statement

Write a C++ program to compare two numbers using relational operators.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int num1, num2;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    cout << "num1 > num2 = "
         << (num1 > num2) << endl;

    cout << "num1 < num2 = "
         << (num1 < num2) << endl;

    cout << "num1 == num2 = "
         << (num1 == num2);

    return 0;
}

Sample Input

Enter first number: 15
Enter second number: 20

Sample Output

num1 > num2 = 0
num1 &lt; num2 = 1
num1 == num2 = 0

Explanation

Relational operators compare two values and return:

  • 1 → True
  • 0 → False

Concepts Covered

  • Relational Operators
  • Boolean Values
  • Comparison Operators

4. C++ Program to Demonstrate Assignment Operators

Problem Statement

Write a C++ program to demonstrate the use of assignment operators.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number = 20;

    number += 5;
    cout << "After += : "
         << number << endl;

    number -= 3;
    cout << "After -= : "
         << number << endl;

    number *= 2;
    cout << "After *= : "
         << number << endl;

    number /= 4;
    cout << "After /= : "
         << number;

    return 0;
}

Sample Output

After += : 25
After -= : 22
After *= : 44
After /= : 11

Explanation

Assignment operators perform an operation and assign the result back to the same variable.

Concepts Covered

  • Assignment Operators
  • Compound Assignment
  • Variable Updates

5. C++ Program to Demonstrate Increment and Decrement Operators

Problem Statement

Write a C++ program to demonstrate pre-increment, post-increment, pre-decrement, and post-decrement operators.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number = 10;

    cout << "Initial Value = "
         << number << endl;

    cout << "Pre Increment = "
         << ++number << endl;

    cout << "Post Increment = "
         << number++ << endl;

    cout << "Current Value = "
         << number << endl;

    cout << "Pre Decrement = "
         << --number << endl;

    cout << "Post Decrement = "
         << number-- << endl;

    cout << "Final Value = "
         << number;

    return 0;
}

Sample Output

Initial Value = 10
Pre Increment = 11
Post Increment = 11
Current Value = 12
Pre Decrement = 11
Post Decrement = 11
Final Value = 10

Explanation

  • ++number increments before printing.
  • number++ prints first, then increments.
  • --number decrements before printing.
  • number-- prints first, then decrements.

Understanding the difference between pre and post operators is important for interviews and loop-based programming.

Concepts Covered

  • Increment Operator (++)
  • Decrement Operator (--)
  • Pre Increment
  • Post Increment
  • Pre Decrement
  • Post Decrement

6. C++ Program to Demonstrate Logical Operators

Problem Statement

Write a C++ program to demonstrate the use of logical operators (&&, ||, and !).

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int age;

    cout << "Enter your age: ";
    cin >> age;

    cout << "Age >= 18 AND Age <= 60 : "
         << (age >= 18 && age <= 60)
         << endl;

    cout << "Age < 18 OR Age > 60 : "
         << (age < 18 || age > 60)
         << endl;

    cout << "NOT (Age >= 18) : "
         << !(age >= 18);

    return 0;
}

Sample Input

Enter your age: 25

Sample Output

Age >= 18 AND Age &lt;= 60 : 1
Age &lt; 18 OR Age > 60 : 0
NOT (Age >= 18) : 0

Explanation

Logical operators combine multiple conditions.

  • && → True only if both conditions are true.
  • || → True if at least one condition is true.
  • ! → Reverses the result.

Concepts Covered

  • Logical AND
  • Logical OR
  • Logical NOT
  • Boolean Expressions

7. C++ Program to Demonstrate Bitwise Operators

Problem Statement

Write a C++ program to demonstrate bitwise operators.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int a = 6;
    int b = 3;

    cout << "a & b = "
         << (a & b) << endl;

    cout << "a | b = "
         << (a | b) << endl;

    cout << "a ^ b = "
         << (a ^ b) << endl;

    cout << "~a = "
         << (~a);

    return 0;
}

Sample Output

a &amp; b = 2
a | b = 7
a ^ b = 5
~a = -7

Explanation

Bitwise operators perform operations directly on the binary representation of integers.

  • & → Bitwise AND
  • | → Bitwise OR
  • ^ → Bitwise XOR
  • ~ → Bitwise NOT

Concepts Covered

  • Bitwise Operators
  • Binary Operations
  • Binary Representation

8. C++ Program to Find the Largest Number Using the Ternary Operator

Problem Statement

Write a C++ program to find the larger of two numbers using the conditional (ternary) operator.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int num1, num2;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    int largest =
        (num1 > num2)
        ? num1
        : num2;

    cout << "Largest Number = "
         << largest;

    return 0;
}

Sample Input

Enter first number: 80
Enter second number: 65

Sample Output

Largest Number = 80

Explanation

The ternary operator is a shorthand alternative to simple if-else statements.

Syntax

condition ? expression1 : expression2;

Concepts Covered

  • Conditional Operator
  • Ternary Operator
  • Decision Making

9. C++ Program to Demonstrate Operator Precedence

Problem Statement

Write a C++ program to understand operator precedence in arithmetic expressions.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int result;

    result = 10 + 5 * 2;

    cout << "Result = "
         << result;

    return 0;
}

Sample Output

Result = 20

Explanation

Multiplication has higher precedence than addition.

The expression is evaluated as:

10 + (5 × 2)
= 10 + 10
= 20

Concepts Covered

  • Operator Precedence
  • Arithmetic Expressions
  • Evaluation Order

10. C++ Program to Evaluate a Mixed Arithmetic Expression

Problem Statement

Write a C++ program to calculate the following expression:

Expression

(a + b) × c / d

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int a, b, c, d;

    cout << "Enter value of a: ";
    cin >> a;

    cout << "Enter value of b: ";
    cin >> b;

    cout << "Enter value of c: ";
    cin >> c;

    cout << "Enter value of d: ";
    cin >> d;

    int result =
        (a + b) * c / d;

    cout << "Result = "
         << result;

    return 0;
}

Sample Input

Enter value of a: 5
Enter value of b: 3
Enter value of c: 4
Enter value of d: 2

Sample Output

Result = 16

Explanation

The expression is evaluated in this order:

  1. Parentheses (a + b)
  2. Multiplication ×
  3. Division /

This follows the operator precedence rules in C++.

Concepts Covered

  • Arithmetic Expressions
  • Parentheses
  • Operator Precedence
  • Expression Evaluation

11. C++ Program to Check Whether a Number is Even or Odd Using the Modulus Operator

Problem Statement

Write a C++ program to determine whether a number is even or odd using the modulus (%) operator.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    if (number % 2 == 0)
    {
        cout << "Even Number";
    }
    else
    {
        cout << "Odd Number";
    }

    return 0;
}

Sample Input

Enter a number: 18

Sample Output

Even Number

Explanation

The modulus operator returns the remainder after division.

  • If number % 2 == 0, the number is Even.
  • Otherwise, it is Odd.

Concepts Covered

  • Modulus Operator
  • Arithmetic Operators
  • Decision Making
  • Integer Division

12. C++ Program to Check Whether a Number is Divisible by Both 5 and 11

Problem Statement

Write a C++ program to check whether a number is divisible by both 5 and 11.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    if (number % 5 == 0 && number % 11 == 0)
    {
        cout << "Number is divisible by both 5 and 11.";
    }
    else
    {
        cout << "Number is NOT divisible by both 5 and 11.";
    }

    return 0;
}

Sample Input

Enter a number: 55

Sample Output

Number is divisible by both 5 and 11.

Explanation

The logical AND (&&) operator ensures that both conditions must be true.

Concepts Covered

  • Logical AND
  • Modulus Operator
  • Multiple Conditions

13. C++ Program to Find the Maximum of Three Numbers Using Operators

Problem Statement

Write a C++ program to find the largest among three numbers using relational operators.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int a, b, c;

    cout << "Enter three numbers: ";
    cin >> a >> b >> c;

    if (a >= b && a >= c)
    {
        cout << "Largest Number = " << a;
    }
    else if (b >= a && b >= c)
    {
        cout << "Largest Number = " << b;
    }
    else
    {
        cout << "Largest Number = " << c;
    }

    return 0;
}

Sample Input

Enter three numbers: 45 62 31

Sample Output

Largest Number = 62

Explanation

The program compares three numbers using relational and logical operators to determine the largest value.

Concepts Covered

  • Relational Operators
  • Logical Operators
  • Comparison Logic

14. C++ Program to Calculate Percentage of Five Subjects

Problem Statement

Write a C++ program to calculate the percentage of marks obtained in five subjects.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float s1, s2, s3, s4, s5;
    float percentage;

    cout << "Enter marks of five subjects: ";
    cin >> s1 >> s2 >> s3 >> s4 >> s5;

    percentage =
        (s1 + s2 + s3 + s4 + s5) / 5;

    cout << "Percentage = "
         << percentage << "%";

    return 0;
}

Sample Input

Enter marks of five subjects:
80
90
75
85
70

Sample Output

Percentage = 80%

Explanation

The percentage is calculated by adding all subject marks and dividing by the total number of subjects.

Concepts Covered

  • Arithmetic Operators
  • Average Calculation
  • Float Variables

15. C++ Program to Calculate Electricity Bill Using Operators

Problem Statement

Write a C++ program to calculate the electricity bill based on the following rate:

  • Cost per unit = ₹8

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float units;
    float bill;

    cout << "Enter electricity units consumed: ";
    cin >> units;

    bill = units * 8;

    cout << "Total Electricity Bill = Rs. "
         << bill;

    return 0;
}

Sample Input

Enter electricity units consumed: 125

Sample Output

Total Electricity Bill = Rs. 1000

Explanation

The bill amount is calculated by multiplying the total units consumed by the fixed cost per unit.

Formula

Electricity Bill = Units × Rate per Unit

Concepts Covered

  • Multiplication Operator
  • Variables
  • Formula-Based Programming
  • User Input

Chapter Summary

In this chapter, you explored the different types of operators available in C++. You learned how arithmetic, relational, logical, assignment, increment/decrement, bitwise, and conditional operators work. Through practical programming examples, you also understood operator precedence, expression evaluation, and how operators are used in real-world calculations such as percentage, electricity bill, and divisibility checks. These concepts are essential for writing efficient C++ programs and form the foundation for decision-making, loops, and advanced programming topics.


Key Takeaways

  • Arithmetic operators perform mathematical calculations.
  • Relational operators compare values and return true or false.
  • Logical operators combine multiple conditions.
  • Assignment operators simplify updating variable values.
  • Increment and decrement operators modify variable values efficiently.
  • Bitwise operators work directly with binary representations.
  • The ternary operator provides a shorthand alternative to simple if-else statements.
  • Operator precedence determines the order in which expressions are evaluated.
  • Parentheses can be used to control evaluation order.
  • Understanding operators is essential before learning conditional statements and loops.

Frequently Asked Questions (FAQs)

1. What are operators in C++?

Operators are symbols used to perform operations such as arithmetic calculations, comparisons, logical operations, assignments, and bitwise manipulation.


2. What are the main types of operators in C++?

The main categories are:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Increment/Decrement Operators
  • Bitwise Operators
  • Conditional (Ternary) Operator

3. What is the difference between = and ==?

  • = assigns a value to a variable.
  • == compares two values for equality.

4. What is the modulus (%) operator used for?

The modulus operator returns the remainder after dividing one integer by another. It is commonly used to check divisibility and determine whether a number is even or odd.


5. What is operator precedence?

Operator precedence defines the order in which operators are evaluated in an expression. Multiplication and division are evaluated before addition and subtraction unless parentheses change the order.


6. What is the ternary operator?

The ternary operator (? :) is a shorthand way to write simple if-else conditions in a single line.


7. Why are logical operators important?

Logical operators allow you to combine multiple conditions and are widely used in decision-making statements such as if, else, and loops.


8. Why should beginners practice operator-based programs?

Operator-based programs help build a strong foundation in expressions, calculations, comparisons, and logical thinking, which are essential for mastering C++ programming and solving real-world coding problems.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top