How to solve increment and decrement operators in c?
Answers
[25/02, 1:12 pm] vishaltamta30: Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
[25/02, 1:13 pm] vishaltamta30: Syntax:
Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
[25/02, 1:13 pm] vishaltamta30: Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
[25/02, 1:13 pm] vishaltamta30: EXAMPLE PROGRAM FOR INCREMENT OPERATORS IN C:
In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.
[25/02, 1:13 pm] vishaltamta30: #include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
[25/02, 1:14 pm] vishaltamta30: OUTPUT:
1 2 3 4 5 6 7 8 9
[25/02, 1:14 pm] vishaltamta30: EXAMPLE PROGRAM FOR DECREMENT OPERATORS IN C:
In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator and output is displayed as “20 19 18 17 16 15 14 13 12 11”.
C
//Example for decrement operators
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
//Example for decrement operators
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
OUTPUT:
20 19 18 17 16 15 14 13 12 11