0% found this document useful (0 votes)
30 views

R23 Ip Unit I Part Ii

The document discusses different types of operators in C programming language including arithmetic, assignment, relational, unary, and bitwise operators. It provides examples of using each operator type and explains their functionality. Specifically, it covers addition, subtraction, multiplication, division, and modulus arithmetic operators. It also discusses unary operators like increment and decrement and shows the difference between pre and post versions. Relational operators for comparison like equal, not equal, less than, and greater than are described along with examples.

Uploaded by

naga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

R23 Ip Unit I Part Ii

The document discusses different types of operators in C programming language including arithmetic, assignment, relational, unary, and bitwise operators. It provides examples of using each operator type and explains their functionality. Specifically, it covers addition, subtraction, multiplication, division, and modulus arithmetic operators. It also discusses unary operators like increment and decrement and shows the difference between pre and post versions. Relational operators for comparison like equal, not equal, less than, and greater than are described along with examples.

Uploaded by

naga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming for Problem Solving using C UNIT-1

OPERATORS:
An operator is a symbol which represents a particular operation that can be performed on
data. The data itself (which can be either a variable or constant) is called operand.
Expressions made by combining operators and operands.
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Unary operators
5. Bitwise operators
6. Logical/Boolean operators
7. Conditional operators
8. Special operators

1. Arithmetic operations:
The arithmetic operators in „C‟ language are +(addition), -(subtraction),
*(multiplication), / (division), % (modulus) these operators are called arithmetic/binary
operators. Each operand can be int, float, char.
Assume variable A holds 10 and variable B holds 20 then –

Operator Description Example


+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = 10

∗ Multiplies both operands. A ∗ B = 200

∕ Divides numerator by de-numerator. B∕A=2


Modulus Operator and remainder of after an integer
% B%A=0
division.

Example:
#include <stdio.h>
void main() {
int a = 21;
int b = 10;
int c ;
c = a + b;
printf("Addition is %d\n", c );
c = a - b;
printf("Subtraction is %d\n", c );
c = a * b;
printf("Multiplication is %d\n", c );
c = a % b;
printf("Reminder is %d\n", c );
}

Page 1
Programming for Problem Solving using C UNIT-1

2. Assignment operators:
These are used to assign the result of the expression to a variable, assignment operator is
„=‟
Syntax: variable operator=Expression
Operator Description Example
C = A + B will
Simple assignment operator. Assigns values from
= assign the value of
right side operands to left side operand
A + B to C
Add AND assignment operator. It adds the right C += A is
+= operand to the left operand and assign the result to equivalent to C =
the left operand. C+A
Subtract AND assignment operator. It subtracts the C -= A is
-= right operand from the left operand and assigns the equivalent to C =
result to the left operand. C-A
Multiply AND assignment operator. It multiplies the C *= A is
*= right operand with the left operand and assigns the equivalent to C =
result to the left operand. C*A
Divide AND assignment operator. It divides the left C /= A is
/= operand with the right operand and assigns the result equivalent to C =
to the left operand. C/A
Modulus AND assignment operator. It takes modulus C %= A is
%= using two operands and assigns the result to the left equivalent to C =
operand. C%A
C <<= 2 is same as
<<= Left shift AND assignment operator.
C = C << 2
C >>= 2 is same as
>>= Right shift AND assignment operator.
C = C >> 2
C &= 2 is same as
&= Bitwise AND assignment operator.
C=C&2
C ^= 2 is same as
^= Bitwise exclusive OR and assignment operator.
C=C^2
C |= 2 is same as C
|= Bitwise inclusive OR and assignment operator.
=C|2

Example:
#include <stdio.h>
void main() {
int a = 21;
int b = 5;
int c = 8;
a += b;
printf("Addition is %d\n", a );
c -= b;
printf("Subtraction is %d\n", c );
c = a * b;
}

Page 2
Programming for Problem Solving using C UNIT-1

3. Relational operators:
The relational operators are used to test or compare the values between two operands.
The relational or equality operators produce an integer value to express the condition of
the comparison. If the condition is false then the integer result is „0‟. Otherwise, the
integer result is nonzero.
The following table shows all the relational operators supported by C language.
Assume variable A holds 10 and variable B holds 20 then –

Operator Description Example


Checks if the values of two operands are equal or not. If (A == B) is
==
yes, then the condition becomes true. not true.
Checks if the values of two operands are equal or not. If (A != B) is
!=
the values are not equal, then the condition becomes true. true.
Checks if the value of left operand is greater than the
(A > B) is
> value of right operand. If yes, then the condition
not true.
becomes true.
Checks if the value of left operand is less than the value (A < B) is
<
of right operand. If yes, then the condition becomes true. true.
Checks if the value of left operand is greater than or
(A >= B) is
>= equal to the value of right operand. If yes, then the
not true.
condition becomes true.
Checks if the value of left operand is less than or equal to
(A <= B) is
<= the value of right operand. If yes, then the condition
true.
becomes true.

Example:

#include <stdio.h>
void main() {
int a = 10;
int b = 20;
printf("Equals %d \n", (a==b) );
printf("Not Equals %d \n", (a!=b) );
printf("Less than %d \n", (a<b) );
printf("Greater than %d \n", (a>b) );
}

Page 3
Programming for Problem Solving using C UNIT-1

4. Unary operators:
The unary ‗++„ and ‗- -‗operators increment or decrement the value in a
variable by 1. There are ‗pre„ and ‗post„ variants for both operators that do slightly
different things as explained below.
Var++ post increment
++Var pre increment Var-
- post decrement
- -Var pre decrement

Post Pre Post Pre


Increment Increment Decrement Decrement
a=5 a=5 a=5 a=5
b = a++ b = ++a b = a-- b = --a
b=5 b=6 b=5 b=5
a=6 a=6 a=4 a=4

Example-1: Example-2:
#include <stdio.h> #include <stdio.h>
void main( ) { void main( ) {
int a = 5; int a = 5;
printf("a = %d \n", a ); printf("a = %d \n", a );
printf("a = %d \n", a++ ); printf("a = %d \n", ++a );
printf("a = %d \n", a ); printf("a = %d \n", a );
} }
Output: Output:
a=5 a=5
a=5 a=6
a=6 a=6
Example-3: Example-4:
#include <stdio.h> #include <stdio.h>
void main( ) { void main( ) {
int a = 5; int a = 5;
printf("a = %d \n", a ); printf("a = %d \n", a );
printf("a = %d \n", a-- ); printf("a = %d \n", --a );
printf("a = %d \n", a ); printf("a = %d \n", a );
} }
Output: Output:
a=5 a=5
a=5 a=4
a=4 a=4

Page 4
Programming for Problem Solving using C UNIT-1

5. Bitwise operators:
A smallest element in the memory on which they are able to operator is a bit, C suppose several
bitwise operators.
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101

A&B = 0000 1100


A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The following table lists the bitwise operators supported by C.
Assume variable 'A' holds 60 and variable 'B' holds 13, then −
Operator Description Example
Binary AND Operator copies a bit to the result if it (A & B) = 12,
& exists in both operands. i.e., 0000 1100
Binary OR Operator copies a bit if it exists in (A | B) = 61,
| either operand. i.e., 0011 1101
Binary XOR Operator copies the bit if it is set in (A ^ B) = 49,
^ one operand but not both. i.e., 0011 0001
Binary One's Complement Operator is unary and (~A ) = ~(60),
~ has the effect of 'flipping' bits. i.e,. -0111101
Binary Left Shift Operator. The left operands value
A << 2 = 240
<< is moved left by the number of bits specified by the
i.e., 1111 0000
right operand.
Binary Right Shift Operator. The left operands
A >> 2 = 15
>> value is moved right by the number of bits
i.e., 0000 1111
specified by the right operand.

Example:
#include <stdio.h>
void main( ) {
int a = 60;
int b = 13;
printf("and = %d \n", a&b );
printf("or = %d \n", a|b );
printf("xor = %d \n", a^b );
printf("Not a = %d \n", ~a );
}

Page 5
Programming for Problem Solving using C UNIT-1

6. Logical/Boolean operators:
These operators are used to compare two or more operands. The logical operator is also called unary
operators. Assumevariable A holds1 andvariable B holds0, then−
Operator Description Example
Called Logical AND operator. If both the operands are (A && B)
&&
non-zero, then the condition becomes true. is false.
Called Logical OR Operator. If any of the two operands is (A || B) is
||
non-zero, then the condition becomes true. true.
Called Logical NOT Operator. It is used to reverse the
!(A &&
! logical state of its operand. If a condition is true, then
B) is true.
Logical NOT operator will make it false.

Example:
#include <stdio.h>
void main( )
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("Logical and is %d \n", result);
result = (a == b) || (c > b);
printf("Logical or is %d \n", result);
result = !(a == b);
printf("Logical Not is %d \n", result);
}

7. Conditional operators:
The conditional operator ? and : are sometimes called ternary operator since it operands on three
operands, and it is considerhas IF-THEN-ELSEin C statement.

Example:
#include <stdio.h>
voidmain()
{
int n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
printf("Biggestis%d",max);
}

Page 6
Programming for Problem Solving using C UNIT-1

8. Special operators:
Comma Operator
Comma operators are used to link related expressions together. For example:
int a,b = 5, d;
Address operator:
The address of the operator ( & ) returns the address of the variable.
printf ( “Address is %d”, &a );
The sizeof operator
The sizeof is a unary operator that returns the size of data (constants, variables, array,
structure, etc).
Example:
#include <stdio.h>
void main( )
{
int a;
float b;
double c;
char d;
printf("Size of int = %d bytes\n", sizeof(a));
printf("Size of float = %d bytes\n", sizeof(b));
printf("Size of double = %d bytes\n", sizeof(c));
printf("Size of char = %d byte\n", sizeof(d));
}

Decision Making:
Decision making structures require that the programmer specifies one or more
conditions to be evaluated or tested by the program, along with a statement or statements to
be executed if the condition is determined to be true, and optionally, other statements to be
executed if the condition is determined to be false.

1. Simple if Statement:
An if statement consists of a Boolean expression followed by one or more statements.

Page 7
Programming for Problem Solving using C UNIT-1

Syntax:
if ( condition )
{
/* statement(s) will execute if the boolean expression is true */
}
 If the Boolean expression evaluates to true, then the block of code inside the 'if' statement
will be executed.
 C programming language assumes any non-zero and non-null values as true, and if it is
either zero or null, then it is assumed as false value.

Example:
#include<stdio.h>
void main()
{
int a,b;
a=5;
b=6;
if(a<b)
{
printf("B is Big");
}
}

2. if….else statement:
An if statement can be followed by an optional else statement, which executes when
the Boolean expression is false.

Page 8
Programming for Problem Solving using C UNIT-1

Syntax:
if ( condition ) {
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}

 If the Boolean expression evaluates to true, then the if block will be executed, otherwise,
the else block will be executed.
 C programming language assumes any non-zero and non-null values as true, and if it is
either zero or null, then it is assumed as false value.

Example:
#include<stdio.h>
void main()
{
int a,b;
a=6;
b=3;
if(a<b)
{
printf("B is Big");
}
else
{
printf("A is Big");
}
}
3. else….if ladder statement:
An if statement can be followed by an optional else if and else statements.

Page 9
Programming for Problem Solving using C UNIT-1

Syntax:
if ( condition 1 ) {
/* statement(s) will execute if the boolean expression is true */
} else if ( condition 2 ){
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}

 If the Boolean expression evaluates to true, then the if block will be executed, otherwise,
another If the Boolean expression evaluates to true, if that expression is also false then
the else block will be executed.
 C programming language assumes any non-zero and non-null values as true, and if it is
either zero or null, then it is assumed as false value.
Example:
#include<stdio.h>
void main()
{
int a,b;
a=7;
b=7;
if(a<b)
{
printf("B is Big");
}
else if(a>b)
{
printf("A is Big");
}
else
{
printf("Both are Same");
}
}
4. Nested if statement:
The nest if-else statements means you can use one if or else if statement inside
another if or else if statement(s).
Syntax:
if( boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}

Page 10
Programming for Problem Solving using C UNIT-1

Example:
#include <stdio.h>
void main()
{
int age;
printf("Enter age value : ");
scanf("%d", &age);

if (age <= 30 )
{
if(age < 12)
printf("Person is Kid");
else
printf("Person is Adult");
}
else if (age > 30 && age < 60 )
{
printf("Person is Middle Aged");
}
else
{
printf("Person is Old");
}
}
5. Switch Statement:
 The switch statement allows us to execute one code block among many alternatives.
 You can do the same thing with the if...else..if ladder. However, the syntax of
the switch statement is much easier to read and write.
Syntax:
switch(Expression)
{
case constant-1 : statements;
break;
case constant-2 : statements;
break;
case constant-3 : statements;
break;
.
.
.
default : statements;
break;
}

Page 11
Programming for Problem Solving using C UNIT-1

Example:
#include<stdio.h>
void main()
{
int wk;
printf("Enter week value: ");
scanf("%d",&wk);
switch(wk)
{
case 1: printf("Monday");
break;
case 2: printf("Tuesday");
break;
case 3: printf("Wednesday");
break;
case 4: printf("Thursday");
break;
case 5: printf("Friday");
break;
case 6: printf("Saturday");
break;
case 7: printf("Sunday");
break;
default: printf("Invalid");
break;
}
}
Iterative Statements:
 In programming, loops are used to repeat a block of code until a specified condition is
met.
 C programming has three types of loops.
1. while loop
2. do...while loop
3. for loop
1. while loop:
 The while loop evaluates the test expression inside the parenthesis ( ).
 If the test expression is true, statements inside the body of while loop are executed.
Then, the test expression is evaluated again.
 The process goes on until the test expression is evaluated to false.
 If the test expression is false, the loop terminates (ends).

Page 12
Programming for Problem Solving using C UNIT-1

Syntax:
while ( condition )
{
// statements inside the body of the loop.
}

Example:
#include<stdio.h>
void main()
{
int n, i;
printf("Enter n value: ");
scanf("%d",&n);
i = 1;
while( i < n )
{
printf("%d ",i);
i++;
}
}
2. while loop:
 The body of do...while loop is executed once. Only then, the test expression is
evaluated.
 If the test expression is true, the body of the loop is executed again and the test
expression is evaluated.
 This process goes on until the test expression becomes false.
 If the test expression is false, the loop ends.

Syntax:
do
{
// statements inside the body of the loop.
} while ( condition ) ;

Page 13
Programming for Problem Solving using C UNIT-1

Example:
#include<stdio.h>
void main()
{
int n, i;
printf("Enter n value: ");
scanf("%d",&n);
i=1;
do
{
printf("%d ",i);
i++;
}while(i<n);
}
3. for loop:
 The initialization statement is executed only once.
 Then, the test expression is evaluated. If the test expression is evaluated to false,
The for loop is terminated.
 However, if the test expression is evaluated to true, statements inside the body
of for loop are executed, and the update expression is updated.
 Again the test expression is evaluated.
 This process goes on until the test expression is false. When the test expression is
false, the loop terminates.

Syntax:
for ( initialization ; condition ; update statement )
{
// statements inside the body of the loop
}

Page 14
Programming for Problem Solving using C UNIT-1

Example:
#include<stdio.h>
void main()
{
int n, i;
printf("Enter n value: ");
scanf("%d",&n);
for(i=1;i<n;i++)
{
printf("%d ",i);
}
}
1. i is initialized to 1.
2. The test expression i < 9 is evaluated. Since 1 less than 11 is true, the body of for loop
is executed. This will print the 1 (value of i) on the screen.
3. The update statement i++ is executed. Now, the value of i will be 2. Again, the test
expression is evaluated to true, and the body of for loop is executed. This will
print 2 (value of i) on the screen.
4. Again, the update statement i++ is executed and the test expression i < 9 is evaluated.
This process goes on until i becomes 9.
5. When i becomes 11, i < 9 will be false, and the for loop terminates.

Break and continue statements:

1. Break Statement:

The break statement ends the loop immediately when it is encountered. Its syntax is:
break;
The break statement almost used with if….else statement inside the loop.

Page 15
Programming for Problem Solving using C UNIT-1

Example:
#include<stdio.h>
void main()
{
int i;
for(i=1;i<10;i++)
{
if( i == 6)
break;
printf("%d ",i);
}
}

2. Continue Statement:
The continue statement skips the current iteration of the loop and continues with the
next iteration. Its syntax is:
continue;
The continue statement almost used with if….else statement inside the loop.

Example:
#include<stdio.h>
void main()
{
int i;
for(i=1;i<10;i++)
{
if( i == 6)
continue;
printf("%d ",i);
}
}

Page 16
Programming for Problem Solving using C UNIT-1

1. Calculator Program:
#include<stdio.h>
void main()
{
int op, a, b, c;
float d;
while(1)
{
printf("\n\n 1.Add 2.Sub 3.Mul 4.Div 5.exit");
printf("\n Choose operator: ");
scanf("%d", &op);
switch(op)
{
case 1:
printf("Enter a Value: ");
scanf("%d", &a);
printf("Enter b Value: ");
scanf("%d", &b);
c = a + b;
printf("Addition is %d ", c );
break;
case 2:
printf("Enter a Value: ");
scanf("%d", &a);
printf("Enter b Value: ");
scanf("%d", &b);
c = a - b;
printf("Subtraction is %d ", c );
break;
case 3:
printf("Enter a Value: ");
scanf("%d", &a);
printf("Enter b Value: ");
scanf("%d", &b);
c = a * b;
printf("Multiplication is %d ", c );
break;
case 4:
printf("Enter a Value: ");
scanf("%d", &a);
printf("Enter b Value: ");
scanf("%d", &b);
d = (float) a / b;
printf("Division is %.2f ", d );

Page 17
Programming for Problem Solving using C UNIT-1

break;
case 5: exit(0);
default:
printf("Invalid choice");
break;
}
}
}
Output:
1. Add 2.Sub 3.Mul 4.Div 5.exit
Choose operator: 1
Enter a Value: 12
Enter b Value: 15
Addition is 27

1.Add 2.Sub 3.Mul 4.Div 5.exit


Choose operator: 2
Enter a Value: 19
Enter b Value: 12
Subtraction is 7

1.Add 2.Sub 3.Mul 4.Div 5.exit


Choose operator: 3
Enter a Value: 15
Enter b Value: 6
Multiplication is 90

1.Add 2.Sub 3.Mul 4.Div 5.exit


Choose operator: 4
Enter a Value: 52
Enter b Value: 6
Division is 8.67

1.Add 2.Sub 3.Mul 4.Div 5.exit


Choose operator: 8
Invalid choice

1. Add 2.Sub 3.Mul 4.Div 5.exit


Choose operator: 5

Page 18
Programming for Problem Solving using C UNIT-1

2. Factorial Program:
#include<stdio.h>
void main()
{
int n, i;
long int f;
printf("Enter n value: ");
scanf("%d", &n);
f = 1;
for(i=1;i<=n;i++)
{
f = f * i;
}
printf("Factorial is %d", f );
}

3. Fibonacci Program:
#include<stdio.h>
void main()
{
int f1, f2, f3, n, i;
printf("Enter n value: ");
scanf("%d", &n);
f1 = 0;
f2 = 1;
printf(" %d %d", f1, f2 );
for(i=2;i<n;i++)
{
f3 = f1 + f2;
printf(" %d", f3 );
f1 = f2;
f2 = f3;
}
}

Page 19
Programming for Problem Solving using C UNIT-1

4. Write a Program to print following series.


1
12
123
1234
12345
Program:
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
}
5. Write a C program that accepts 4 integers p, q, r, s from the user where r and s are positive
and p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater
than the sum of p and q print "Correct values", otherwise print "Wrong values".
Program:
#include<stdio.h>
void main()
{
int p, q, r, s;
printf("Enter p value: ");
scanf("%d",&p);
printf("Enter q value: ");
scanf("%d",&q);
printf("Enter r value: ");
scanf("%d",&r);
printf("Enter s value: ");
scanf("%d",&s);
if( r>=0 && s>=0 && p%2==0 && q>r && s>p && r+s > p+q)
{
printf("Correct Values");
}
else
{
printf("Wrong Values");
}
}

Page 20

You might also like