w3resource

C++ Exercises: Display the operation of pre and post increment and decrement


Pre and Post Increment/Decrement

Write a C++ program to display the operation of pre and post increment and decrement.

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    int num = 57; // Initializing an integer variable 'num' with the value 57
    cout << "\n\n Display the operation of pre and post increment and decrement :\n"; // Outputting a message for demonstrating increment and decrement operations
    cout << "--------------------------------------------------------------------\n"; // Outputting a separator line    
    cout <<" The number is : " << num << endl; // Displaying the initial value of 'num' 
    num++; // Post-incrementing 'num' by 1
    cout <<" After post increment by 1 the number is : " << num << endl; // Displaying 'num' after post-incrementing
    ++num; // Pre-incrementing 'num' by 1
    cout <<" After pre increment by 1 the number is : " << num << endl; // Displaying 'num' after pre-incrementing
    num = num + 1; // Increasing 'num' by 1
    cout <<" After increasing by 1 the number is : " << num << endl; // Displaying 'num' after increasing by 1
    num--; // Post-decrementing 'num' by 1
    cout <<" After post decrement by 1 the number is : " << num << endl; // Displaying 'num' after post-decrementing
    --num; // Pre-decrementing 'num' by 1
    cout <<" After pre decrement by 1 the number is : " << num << endl; // Displaying 'num' after pre-decrementing
    num = num - 1; // Decreasing 'num' by 1
    cout <<" After decreasing by 1 the number is : " << num << endl; // Displaying 'num' after decreasing by 1
    cout << endl; // Outputting a blank line for better readability
    return 0; // Returning 0 to indicate successful program execution
} // End of the main function

Sample Output:

 Display the operation of pre and post increment and decrement :       
--------------------------------------------------------------------   
 The number is : 57                                                    
 After post increment by 1 the number is : 58                          
 After pre increment by 1 the number is : 59                           
 After increasing by 1 the number is : 60                              
 After post decrement by 1 the number is : 59                          
 After pre decrement by 1 the number is : 58                           
 After decreasing by 1 the number is : 57 

Flowchart:

Flowchart: Display the operation of pre and post increment and decrement

For more Practice: Solve these Related Problems:

  • Write a C++ program to compare the outputs of pre and post increment operators within a single complex expression.
  • Write a C++ program that uses both pre and post decrement operators in a loop and displays the intermediate results.
  • Write a C++ program to demonstrate the effect of pre and post increment operators on an array index during iteration.
  • Write a C++ program that uses nested expressions involving both pre and post increment/decrement operators to manipulate a variable.

Go to:


PREV : Overflow and Underflow in Arithmetic.
NEXT : Output Formatting Example.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.