C++ exercises and solutions: increment decrement operators


C++ increment decrement operators

1. Write a C++ program to produce the output as shown below:


 
Contents of x               Expression        Value of                Contents of x

before                                                Expression                   after

5                 |                  x++             |         5                 |         6

5                 |                  x--               |         5                 |         4       

5                 |                  ++x             |         6                 |         6  

5                 |                  --x               |         4                 |         4

Solution:


#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    int x;
   
    x=5;

    cout<<"Contents of x\t"<<"Expression\t"<<"Value of\t"<<"Contents of x\n";

    cout<<"before\t\t\t\t"<<"Expression\t"<<"after\n";
    cout<<"\n";

    cout<<x<<"   |\t"<<"x++"<<" |\t\t\t"<<x<<"\t"<<"\t|"<<"x="<<x+1<<"\n";

    cout<<x<<"   |\t"<<"x--"<<" |\t\t\t"<<x<<"\t"<<"\t|"<<"x="<<x-1<<"\n";

    cout<<x<<"   |\t"<<"++x"<<" |\t\t\t"<<x+1<<"\t"<<"\t|"<<"x="<<x+1<<"\n";

    cout<<x<<"   |\t"<<"--x"<<" |\t\t\t"<<x-1<<"\t"<<"\t|"<<"x="<<x-1<<"\n";
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

2. Write a program to prompt the user to input the integral value of a and print out the result as shown below:


 
Result:
The value of a is: 10
……………………..
The value of ++a is: 11
Now the value of a is: 11
 
The value of a++ is: 11
Now the value of a is: 12
 
The value of --a is:11
Now the value of a is:11
 
The value of a-- is: 11
Now the value of a is: 10

Solution:


#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    int a;
    cout<<"Enter a value:";
    cout<<"The value of a is:"<<a; 
    cout<<"\n----------------\n"; 
    ++a; 
    cout<<"\nThe value of ++a is:"<<a; 
    cout<<"\nThe value of a is:"<<a; 
    
    cout<<"\nThe value of a++ is:"<<a; 
    a++; 
    cout<<"\nThe value of a is:"<<a; 
    --a; 
    cout<<"\nThe value of --a is:"<<a; 
    cout<<"\nThe value of a is:"<<a; 
    cout<<"\nThe value of a-- is:"<<a; 
    a--; 
    cout<<"\nThe value of a is:"<<a; 
    cout<<"\n"; 

    system("PAUSE"); 
    return EXIT_SUCCESS;

} 


Comments

mahi comment

 mahi

explain
#include<iostream.h>
#include<conio.h>
void main()
{
int n1=10, &n2=n1;
n2++;
cout<<n1;
}


2017-11-12
White Wolf comment

 White Wolf

int main(){
int a = 5;
cout <<"The value of a is "<< a << endl;
cout<<"The value of a after a++ "<<a++<<endl;
return 0;
}
output:
The value of a is 5
The value of a after a++ 5


2017-06-29
muler comment

 muler

it is important for Every C++ interview question


2017-05-09
tahsin comment

 tahsin

what is it


2017-05-02
lavanya comment

 lavanya

Very usefull


2017-04-08
Mohammad Amir comment

 Mohammad Amir

I love c++.


2017-02-16
Taram Nayab comment

 Taram Nayab

There is diff btw --a and a-- , the operation applied on both will be same , but the output depends upon the condition wether its increament or decreament.
for eg if a=5 ,
then
for " --a" answer will be 4 and output will be 4
and
" a-- "answer will be 4 but output will be 5


2016-12-26
Taram Nayab comment

 Taram Nayab

#include <iostream>

using namespace std;
int main()
{
int a;
cout<<"Enter value of a: ";
cin>>a;
cout<<a<<" \t"<<"Value of ++a:"<<a+1<<"\t"<<"now value of a is:"<<a+1<<"\n";
cout<<a<<" \t"<<"Value of a++:"<<a+1<<"\t"<<"now value of a is:"<<a+2<<"\n";
cout<<a<<" \t"<<"Value of --a:"<<a+1<<"\t"<<"now value of a is:"<<a+1<<"\n";
cout<<a<<" \t"<<"Value of a--:"<<a+1<<"\t"<<"now value of a is:"<<a<<"\n";
return 0;
}


2016-12-26
Taram Nayab comment

 Taram Nayab

#include <iostream>

using namespace std;
int main()
{
int a;
cout<<"Enter value of a: ";
cin>>a;
cout<<a<<" \t"<<"Value of ++a:"<<a+1<<"\t"<<"now value of a is:"<<a+1<<"\n";
cout<<a<<" \t"<<"Value of a++:"<<a+1<<"\t"<<"now value of a is:"<<a+2<<"\n";
cout<<a<<" \t"<<"Value of --a:"<<a+1<<"\t"<<"now value of a is:"<<a+1<<"\n";
cout<<a<<" \t"<<"Value of a--:"<<a+1<<"\t"<<"now value of a is:"<<a<<"\n";
return 0;
}


2016-12-26
naruto005 comment

 naruto005

@Asif Raza
no there is no difference in a-- & --a.
The only thing Matter How and Where you used those decrement or Increment Operators.


2016-04-02
Nassim DAI comment

 Nassim DAI

li dayr DAI o rah hna islemli 3la drari dial esto
cout<<"peace";


2016-03-20
Asif Raza  comment

 Asif Raza

no defference between __a,&a__?


2015-11-07
Bassam  comment

 Bassam

For exercise 2, there are 2 problems.
1.There needs to be a "a cin statement for value entry of a ".
2.the increment operators should not in separate lines of code , they should be included in the same cout statement. e.g (cout<<"the value for a++ is:"<<a++;)


2015-06-21
guerishom comment

 guerishom

it is so helpfull


2015-06-10
Amin comment

 Amin

this is awesome


2015-04-26
Mahroz comment

 Mahroz

hahahahahahahhahahahahhaha


2015-04-12



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.