C Language

C programming increment & decrement operator and Exercise

C programming increment & decrement Exercise

In this post, we will discuss the C programming increment &dicrement operator and Exercise

increment and decrement Operators

Increment operators are used to increasing the value of the variable and decrement operators are used to decrease the value of the variable in C programs
syntex:
increment operator :++var_name; or var_name++;
decrement operator :–var_name; or var_name–;
Example
increment operator :  ++x;  x++;

decrement operator :  –y;    y++;

The following table displays an explanation on increment and decrement operators
Operators
The increment operator ++ adds 1 to its variable
x=x+1;                                                                    x=x+1;
is the same as                               So                       can be written
x++;                                                                        ++x;
The decrement operator —  remove 1 to its variable
x=x-1;                                                                    x=x-1;
is the same as                               So                       can be written
x–;                                                                        –x;

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;
}

output
Example

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;
}

Example

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;
}

Example

 

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;
}

Example
When the above code is executed, it produces the following result
Output

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

Output

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;
}

When the above code is executed, it produces the following result
Output
Suggested for you
Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.