Thursday, January 20, 2011

Arithmetic Operators


C programming language supports almost common arithmetic operator such as +,-,* and modulus operator %. Modulus operator (%) returns the remainder of integer division calculation. The operators have precedence rules which are the same rule in math.
Here is a C program demonstrate arithmetic operators:
01#include <stdio.h>
02/* a program demonstrates C arithmetic operators */
03void main(){
04    int x = 10, y = 20;
05     
06    printf("x = %d\n",x);
07    printf("y = %d\n",y);
08    /* demonstrate = operator + */
09    y = y + x;
10    printf("y = y + x; y = %d\n",y);
11  
12    /* demonstrate - operator */
13    y = y - 2;
14    printf("y = y - 2; y = %d\n",y);
15    /* demonstrate * operator */
16    y = y * 5;
17    printf("y = y * 5; y = %d\n",y);
18  
19    /* demonstrate / operator */
20    y = y / 5;
21    printf("y = y / 5; y = %d\n",y);
22  
23    /* demonstrate modulus operator % */
24    int remainder = 0;
25    remainder = y %3;
26  
27    printf("remainder = y %% 3; remainder = %d\n",remainder);
28  
29    /* keep console screen until a key stroke */
30    char key;
31    scanf(&key);
32}
And here is the output
x = 10
y = 20
x; y = 30
y = y
y = y
+- 2; y = 28
= 140
y = y / 5; y
y = y * 5; y
= 28
er = y % 3; remainder = 1
remain
d

Dont Miss Another Post Connect With Us !

Enter your email address:

0 comments: