Computer scienceProgramming languagesC++Operators and expressions

Increment and decrement operators

X squared

Report a typo

Write a C++ program that takes an initial integer value x as input and performs the following operations:

  1. Increment x by 1 using the postfix increment operator.

  2. Decrement x by 1 using the postfix decrement operator.

  3. Use the resulting value of x to calculate the square of x and store it in a variable x_squared.

  4. Print the original value of x, the value of x after increment and decrement, and the calculated x_squared.

Sample Input 1:

0

Sample Output 1:

Original value of x: 0
Value of x after increment: 0
Value of x after decrement: 1
Square of x: 0

Sample Input 2:

4    

Sample Output 2:

Original value of x: 4
Value of x after increment: 4
Value of x after decrement: 5
Square of x: 16

Sample Input 3:

8

Sample Output 3:

Original value of x: 8
Value of x after increment: 8
Value of x after decrement: 9
Square of x: 64
Write a program in C++





Create a free account to access the full topic