Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Programming for Problem Solving

BCAC101

Mr. Subhas Chandra Nath


Assistant Professor
Department of Computer
Application

1 Lecture :: 5
Contents

 Operators in C
 Arithmetic Operator
 Relational Operator
 Logical Operator
 Increment & Decrement Operator
 Hands on Example

2
Operators in C

An expression consists of variables & constants separated by


operators. C language uses many types of operators as listed
below.
 Arithmetic Operator
 Relational Operator
 Logical Operator
 Increment or Decrement Operator
 Assignment Operator
 Conditional Operator
 Bitwise Operator
3
Arithmetic Operator

Following table shows all the arithmetic operators


supported by C language. Assume variable A is 20 and
B is 10, then:
Operator Description Example
+ Add two operands A+B = 30
- Subtract 2nd operand from 1st A-B = 10
* Multiply both operand A*B = 200
/ Divides 1st operand by 2nd A/B = 2
operand
% Find the remainder A%B = 0
4
Continue…

Example 1: Write a C program to reverse a 3 digit number using ‘/’ & ‘%’
operator.
#include<stdio.h>
#include<conio.h>
Output
void main() {
int n,a,b,c,rev;
clrscr();
printf("\nEnter the 3 digit number::");
scanf("%d",&n);
a=n/100; n=n%100; b=n/10; c=n%10;
rev=c*100+b*10+a;
printf("\nReverse Number=%d",rev);
5
getch(); }
Relational Operator

It is used to compare the values of operand to produce a logical


value. A logical value is either true or false. Following table shows
all the relational operators supported by C language.

Operator Meaning Example Result


< Less Than 10<5 false
<= Less Than or Equal to 5<=10 true
> Grater Than 10>5 true
>= Grater Than or Equal to 5>=10 false
== Equal to 5==10 false
!= Not equal to 5!=10 true
6
Logical Operator

Logical operators are used to connect more relational


operations to form a complex expression called logical
expression. A value obtained by evaluating a logical
expression is always logical, means either true or false. C
has the following three logical operator.
Operator Meaning Example Result

&& Logical And (5<2)&&(5>3) false

|| Logical Or (5<2)||(5>3) true

! Logical Not !(5<2) true


7
Increment & Decrement
Operator

The increment operator (++) is used to increase the value


of the operands by 1 where decrement operator (--) is
used to reduce the value of the operands by 1. Both are
unary operators & take the following forms:
++a or a++ [means a=a+1]
--a or a-- [means a=a-1]
Example:
int x=5, y=10;
x++; // means x=6
8
y--; // means y=9
Continue…

++a and a++ means the same thing when they form
statements independently & they behave differently
when they are used in expression or the R.H.S. of an
assignment statement.
Consider the following example
int x=10,y,z;
y=++x; // pre increment
printf (“X=%d Y=%d”, x,y); //X=11 & Y=11
z=x++; // post increment
9
printf (“X=%d Z=%d”, x,z); //X=12 & Z=11
Continue…

Similarly --a and a-- means the same thing when they
form statements independently & they behave
differently when they are used in expression or the
R.H.S. of an assignment statement.
Consider the following example
int p=50,q,r;
q=--p; // pre decrement
printf (“P=%d Q=%d”, p,q); //P=49 & Q=49
r=p--; // post decrement
10 printf (“P=%d R=%d”, p,r); //P=48 & R=49
Continue…

Example 2: Write a C program to implement the increment & decrement


operator.
#include<stdio.h> Output
#include<conio.h>
void main()
{
int a=10,b=15;
clrscr();
printf(“\n%3d%3d%3d%3d”,a++,++a,a--,--a);
printf(“\n%3d%3d%3d%3d”,++b,b++,--b,b--);
getch();
11 }
Hands on Example

Hands on Examples with Code Blocks

12
Thank You.

Any clarification contact me through e-mail


scn.assignment@gmail.com

13

You might also like