Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C programming increment & decrement operator and Exercise

Posted on November 14, 2016January 29, 2020

Table of Contents

  • C programming increment & decrement Exercise
  • increment and decrement Operators

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
 
C programming increment & decrement Exercise
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
C programming increment & decrement Exercise
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;
}

C programming increment & decrement Exercise
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;
}

C programming increment & decrement Exercise
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;
}

C programming increment & decrement Exercise
Example
When the above code is executed, it produces the following result
C programming increment & decrement Exercise
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

C programming increment & decrement Exercise
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
C programming increment & decrement Exercise
Output
Suggested for you
The operator in Java language
The operator in C language
The operator in C++ language
The operator in Python language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes