Sponsor

banner image

recent posts

banner image

Arithmetic & Relational Operators in C

An operator in C tells the compiler which operation has to be performed. It performed logically or mathematical operation based on the operators in C.

Operators in C is divided into the following categories:

1. Arithmetic Operators in C
3. Bitwise Operators in C
4. Logical Operators in C
5. Assignment Operators in C
6. Conditional Operators in C
7. Misc Operators in C

1. Arithmetic operators in C

Arithmetic operators performed arithmetic operation like addition, subtraction, division, multiplication and modulus.

Addition Operator (+): Adds two operands: (15+20 = 35)
Subtraction Operator (-): Subtact two operands: (20-15 = 5)
Multiplication Operator (*): Multiply two operands: (5*10 = 50)
Division Operator (/): Divide two operands:(10/5=2)
Modulus Operator (%): Divide two operands and keep remainder: (10/3 = 1)

These operations are performed on numeric data types in C.

Program

#include <stdio.h>
int main ()
{
    int a = 10, b = 5; //Initialization of variable 'a' and 'b'
    int add = a+b; //Addition Operator
    printf ("Output of Addition %d\n", add);
    int sub = a-b; //Subtraction Operator
    printf ("Output of Subtraction %d\n",sub);
    int div = a/b; //Division Operator
    printf ("Output of Division %d\n", div);
    int mul = a*b; //Multiplication Operator
    printf ("Output of Multiplication %d\n", mul);
    int mod = 5%2; //Modulus Operator
    printf ("Output of Modulus %d\n",mod);
    return 0;
}

Output

Output of Addition 15
Output of Subtraction  5
Output of Division  2
Output of Multiplication 50
Output of Modulus 1

2. Relational Operators in C

The relational operator is used to check the relation between two variables.
Relational operators are
Equal to (==): This operator checks the value of variables. If the value of two variables is equal then output is true else output is false.
Not equal to (!=): This operator also checks the value of the variables. If the value of the two variables is not equal then output is true otherwise false.
Greater than (>): If the value of operand 'a' is greater than operand 'b'  (a>b) then output is a.
Less than (<): If the value of operand 'a' is less than operand 'b' (a<b) then output is b.
Greater than equal to (>=): If the value of operand 'a' is greater than equal to operand 'b' (a>=b) then output is 'a'.
Less than equal to (<=): If the value of the operand 'a' is less than equal to operand 'b' (a>=b) then output is 'a'.

Program

#include <stdio.h>
int main()
{
    int a = 10, b=11;
    int c = (a==b); //Equal to operator
    printf ("c=%d\n",c);
    int d = (a!=b); //Not equal to operator
    printf ("d=%d\n",d);
    int e = (a>b); //Greater than operator
    printf ("e=%d\n",e);
    int f = (a<b); //Less than operator
    printf ("f=%d\n",f);
    int g = (a>=b); //Greater than equal to operator
    printf ("g=%d\n",g);
    int h = (a<=b); //Less than equal to operator
    printf ("h=%d\n",h);
    return 0;
}

Output

c=0
d=1
e=0
f=1
g=0
h=1






Arithmetic & Relational Operators in C Arithmetic & Relational Operators in C Reviewed by ginnitechblog on 9:00 PM Rating: 5
Powered by Blogger.