Assignment Operators Problem

Assignment Operators Problem

Easy C++ Operators 38 views
Explanation Complexity

Problem Statement

Use compound assignment operators (+=, -=, *=, /=, %=).
Real Life: Like updating your bank balance, score etc.

Input Format

Two integers a and b.

Output Format

Values of a after applying compound assignment operators.

Example

a = 20
b = 5
After +=  a = 25
After -=  a = 15
After *=  a = 100
After /=  a = 4
After %=  a = 0

Constraints

• Integer values

• b should not be 0 for /= and %=

Concept Explanation

Compound assignment operators update a variable using its current value.
They are shorter and cleaner than writing full expressions.
Used in real life for things like bank balance updates or game scores.

Step-by-Step Explanation

1.Start with values a and b.

2.a += b → same as a = a + b.

3.a -= b → same as a = a - b.

4.a *= b → same as a = a * b.

5.a /= b → same as a = a / b.

6.a %= b → same as a = a % b.

7.Print value of a after each operation.

Concept Explanation

Compound assignment operators update a variable using its current value.
They are shorter and cleaner than writing full expressions.
Used in real life for things like bank balance updates or game scores.

Step-by-Step Explanation

1.Start with values a and b.

2.a += b → same as a = a + b.

3.a -= b → same as a = a - b.

4.a *= b → same as a = a * b.

5.a /= b → same as a = a / b.

6.a %= b → same as a = a % b.

7.Print value of a after each operation.

Input / Output Format

Input Format
Two integers a and b.
Output Format
Values of a after applying compound assignment operators.
Constraints
• Integer values

• b should not be 0 for /= and %=

Examples

Input:
a = 20 b = 5
Output:
After += a = 25 After -= a = 15 After *= a = 100 After /= a = 4 After %= a = 0

Example Solution (Public)

C++
void operator_q5_assignment() {
    int num = 10;
    
    cout << "Initial value: " << num << endl;
    
    num += 5;  // num = num + 5
    cout << "After += 5: " << num << endl;
    
    num -= 3;  // num = num - 3
    cout << "After -= 3: " << num << endl;
    
    num *= 2;  // num = num * 2
    cout << "After *= 2: " << num << endl;
    
    num /= 4;  // num = num / 4
    cout << "After /= 4: " << num << endl;
}

Official Solution Code

void operator_q5_assignment() {
    int num = 10;
    
    cout << "Initial value: " << num << endl;
    
    num += 5;  // num = num + 5
    cout << "After += 5: " << num << endl;
    
    num -= 3;  // num = num - 3
    cout << "After -= 3: " << num << endl;
    
    num *= 2;  // num = num * 2
    cout << "After *= 2: " << num << endl;
    
    num /= 4;  // num = num / 4
    cout << "After /= 4: " << num << endl;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.