Table of Contents
C programming increment & decrement Exercise
In this post, we will discuss the C programming increment &dicrement operator and Exercise
increment and decrement Operators
program1
increment using for loop
this program is indicate variable x increment and decrement by one
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=10;
printf(“Hello world!n”);
printf(“x value is %dn”,x++); // x is incremented after assigning it
printf(“x value is %dn”,x);
printf(“x value is %dn”,++x); //x is incremented before assigning it
printf(“x value is %dn”,x);
printf(“x value is %dn”,x–); // x is decremented after assigning it
printf(“x value is %dn”,x);
printf(“x value is %dn”,–x); // x is decremented before assigning it
printf(“x value is %dn”,x);
return 0;
}
program 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=10;
int b;
printf(“Hello world!n”);
printf(“b value is %dn”,x++ + x++);
printf(“b value is %dn”,x);
printf(“b value is %dn”,++x + ++x);
printf(“b value is %dn”,x);
printf(“b value is %dn”,x– – x–);
printf(“b value is %dn”,x);
printf(“b value is %dn”,–x – –x);
printf(“b value is %dn”,x);
return 0;
}
program 3
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=10;
int y=5;
int b;
printf(“Hello world!n”);
printf(“b value is %dn”,x++ + x++ + y++);
printf(“b value is %dn”,x);
printf(“b value is %dn”,y);
printf(“b value is %dn”,++x + ++x + ++y);
printf(“b value is %dn”,y);
printf(“b value is %dn”,x– – x– – y–);
printf(“b value is %dn”,x);
printf(“b value is %dn”,y);
printf(“b value is %dn”,–x – –x – –y);
printf(“b value is %dn”,x);
printf(“b value is %dn”,y);
return 0;
}
program 4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
int y;
int a;
int z;
printf(“Enter a positive integer xn”);
scanf(“%d”,&x);
printf(“(++x) x is incremented value by 1 = %dnn”,++x);
printf(“Enter a positive integer xn”);
scanf(“%d”,&x);
printf(“(–x) x is deccremented value by 1 = %dnn”,–x);
printf(“Enter a positive integer yn”);
scanf(“%d”,&y);
printf(“(y–) y is decremented value by 1 but after assign y = %dnn”,y–);
printf(“Enter a positive integer yn”);
scanf(“%d”,&y);
printf(“(y++) y is incremented value by 1 but after assign y = %dnn”,y++);
return 0;
}
program 5
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
int y;
int a;
int z;
printf(“Enter a positive integer xn”);
scanf(“%d”,&x);
printf(“(++x) x is incremented value by 1 = %dnn”,++x);
printf(“Enter a positive integer xn”);
scanf(“%d”,&x);
printf(“(–x) x is deccremented value by 1 = %dnn”,–x);
printf(“Enter a positive integer yn”);
scanf(“%d”,&y);
printf(“(y–) y is decremented value by 1 but after assign y = %dnn”,y–);
printf(“reperint of x = %dnn”,y); // reperint y value is decremented by 1
printf(“Enter a positive integer yn”);
scanf(“%d”,&y);
printf(“(y++) y is incremented value by 1 but after assign y = %dnn”,y++);
printf(“reperint of x = %dnn”,y); // reperint y value is incremented by 1
return 0;
}
When the above code is executed, it produces the following result
Are you identify what are the different between above the program 4 and program 5
program 6
include <stdio.h>
#include <stdlib.h>
int main()
{
int x;
int y;
printf(“Hello kannan!n”);
printf(“hello enter a number negetive or positive!n”);
scanf(“%d”,&x);
printf(“plese enter incrementel value!n”);
scanf(“%d”,&y);
printf(“your answer is %d”,x+=y);
return 0;
}