C++ Program to Demonstrate Increment and Decrement Operators

Write a Code to demonstrate increment and decrement operators

// Write C++ program to demonstrate various arithmetic operation
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a = 6, ans;

    cout<<"Value of a :--> \n"<<a;

    ans = a++;      //post increment
    cout<<"The value a++ : "<<ans;
    //a is updated after execution of ans

    cout<<"\n\nValue of a :--> "<<a;
    ans = a--;    //post decrement
    cout<<"\nThe value a-- : "<<ans);
    //a is updated after execution of ans

    cout<<"\n\nValue of --a :--> "<<a;
    ans = --a;   //pre decrement
    //a is updated before execution of the ans
    cout<<"\nThe value of --a : "<<ans;

    cout<<"\n\nValue of a :--> "<<a;
    ans = ++a;   //pre increment
    //a is updated before execution of the ans
    cout<<"\nThe value of ++a : %d"<<ans;

    return 0;
}

Output:

Value of a :--> 6
The value a++ : 6

Value of a :--> 7
The value a-- : 7

Value of a :--> 6
The value of --a : 5

Value of a :--> 5
The value of ++a : 6

<< Previous | Next >>