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

POP Module-2

The document discusses various types of operators in C programming language including arithmetic, unary, assignment, relational, and logical operators. It provides examples of using each type of operator and explains their functionality. Specifically, it describes how arithmetic operators perform mathematical operations on values, unary operators act on a single operand, assignment operators assign values, relational operators compare values, and logical operators check true or false conditions. It also gives examples of increment and decrement operators, postfix and prefix expressions, and shorthand assignment operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

POP Module-2

The document discusses various types of operators in C programming language including arithmetic, unary, assignment, relational, and logical operators. It provides examples of using each type of operator and explains their functionality. Specifically, it describes how arithmetic operators perform mathematical operations on values, unary operators act on a single operand, assignment operators assign values, relational operators compare values, and logical operators check true or false conditions. It also gives examples of increment and decrement operators, postfix and prefix expressions, and shorthand assignment operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Module -2

Operators, Decision Control and Looping Statements


Operators:
An operator is simply a symbol that is used to perform operations. In other words, operator is a symbol
in C, such as +, -, &&, ==, which helps the use to perform several mathematical and logical computations. An
operator is used basically to work upon certain data and produce an output as a result of that operation. The
operators included in the C programming language are used to operate not only on numbers but also on data and
variables. The operators in C are classified into various types. They are:
 Arithmetic Operators
 Unary Operators
 Assignment Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Conditional Operators
 Special Operators

Arithmetic Operators:

An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication,


division etc on numerical values (constants and variables).

Operator Meaning of Operator

+ addition

- subtraction

* Multiplication

/ Division

% remainder after division (modulo division)

All these Arithmetic operators in C are binary operators which means they operate on two
operands.

The following table shows all the arithmetic operators supported by the C language. Assume variable A holds 10
and variable B holds 20, then –

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 1


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 division. B%A=0

EXAMPLE PROGRAM FOR C ARITHMETIC OPERATORS:


In this example program, two values ―40‖ and ―20‖ are used to perform arithmetic operations such
as addition, subtraction, multiplication, division, modulus and output is displayed for each operation.
#include <stdio.h>
void main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}

Output of the above program is:


Addition of a, b is: 60
Subtraction of a, b is: 20
Multiplication of a, b is: 800
Division of a, b is: 2
Modulus of a, b is: 0
Unary Operators:
The unary operators in C are used to act upon only one operand, to produce a new value. The unary operators
appear before the operand and are associated with the operand from left to right.
Unary Plus/Minus:
The unary operators are unary plus and unary minus. The most commonly used unary operator in C is the unary

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 2


minus operator. In the unary minus operator, a minus sign precedes the operand assigning a negative value to the
operand.
For example:
a=3 and b=4
c= a + (-b)
In this case, the value of C, when calculated, is -1. This is because b, which initially had a positive value, is
changed to negative when preceded by the unary minus operator.

Increment and Decrement operators:


The increment operator ++ and decrement operator – are unary operators with the same precedence as the
unary -, and they all associate from right to left. Both ++ and -- can be applied to variables, but not to constants or
expressions. The increment operator is used to add 1 to its operand and the decrement operator is used to subtract
1 from its operand. In other words, the increment operator increases the value of an integer by 1 and the
decrement operator decreases the value of the integer by 1. They can occur in either prefix or postfix position,
with possibly different effects occurring. When prefix form is used, the value of the variable is either incremented
or decremented first and is then assigned. However, in the postfix form, the value is used and only after the
assignment operator has performed the operation, that the value is incremented or decremented. These are usually
used with integer data type.

The following table shows increment and decrement operator supported by the C language. Assume
variable A holds 10 ,then –

Operator Description Example

++ Increment operator increases the integer value by one. A++ = 11

-- Decrement operator decreases the integer value by one. A-- = 9

The general syntax is:

++variable; --variable; variable++; variable--;

Some examples are


++count; --k; index++; unit_one--;
We use the increment and decrement statements in for and while loop extensively.

Postfix Expressions:
The postfix expression consists of one operand followed by one operator.
Prefix Expression:
In prefix expression, the operator comes before the operand.

Examples for postfix and prefix expressions:


Consider the following example
m=5;
y=++m;
In this case, the value of y and m would be 6. Suppose, if we rewrite the above statements as
m=5;
Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 3
y=m++;
then the value of y would be 5 and m would 6. A prefix operator first adds to 1 to the operand and then the result
is assigned to the variable on left. On the other hand, a postfix operator first assigns the value to the variable on
left and then increments the operand.

Assignment operators:
Assignment operators are used to assign the result of an expression to a variable. There are
several different assignment operators in C. All of them are used to form assignment expressions which assign the
value of an expression to an identifier. The commonly used assignment operator is ‗=‘ operator. The general
syntax is:

Variable= value or expression;

Where identifier generally represents a variable and expression represents a constant, a variable or a more
complex expression.
For example:
x=y+1;
a=2*c;
rate= 45;
delta= 0.001
sum=a+b;

Note that the assignment operator = and the equality operator == are distinctly different. The assignment operator
is used to assign a value to an identifier, whereas the equality operator is used to determine if two expressions
have the same value. These operators cannot be used in place of one another.
In addition to the usual assignment operator ‗=‘ C has a set of assignment operators called shorthand operators.
Some of the shorthand operators are listed below:
=
+=
-=
*=
/=
%=
>>=
<<=
&=
^=
|=

The following table lists the shorthand assignment operators supported by the C language –

Operator Description Example

= Simple assignment operator. Assigns values C = A + B will assign the value of


from right side operands to left side operand A + B to C

+= Add AND assignment operator. It adds the right C += A is equivalent to C = C + A


operand to the left operand and assign the result

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 4


to the left operand.

-= Subtract AND assignment operator. It subtracts


the right operand from the left operand and C -= A is equivalent to C = C - A
assigns the result to the left operand.

*= Multiply AND assignment operator. It


multiplies the right operand with the left
C *= A is equivalent to C = C * A
operand and assigns the result to the left
operand.

/= Divide AND assignment operator. It divides the


left operand with the right operand and assigns C /= A is equivalent to C = C / A
the result to the left operand.

%= Modulus AND assignment operator. It takes


modulus using two operands and assigns the C %= A is equivalent to C = C % A
result to the left operand.

<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2

|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

Relational operators:
We often compare two quantities and depending on their relation, to take certain decisions. For example, we may
compare the age of two persons, or the price of two items, and so on. These comparisons can be done with the
help of relational operators.
C supports six relational operators in all. These operators and their meanings are shown below:
Operator Meaning
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to
Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 5
== is equal to
!= is not equal to
Given below are some examples of simple relational expressions and their values:
4.5<=10 TRUE
4.5>10 FALSE
-35>=0 FALSE
Relational expressions are used in decision statements such as, if and while to decide the course of action of a
running program.
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. (A == B) is not true.
If yes, then the condition becomes true.

!= Checks if the values of two operands are equal or not. (A != B) is true.


If the values are not equal, then the condition
becomes true.

> Checks if the value of left operand is greater than the (A > B) is not true.
value of right operand. If yes, then the condition
becomes true.

< Checks if the value of left operand is less than the (A < B) is true.
value of right operand. If yes, then the condition
becomes true.

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

<= Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand. If yes, then the
condition becomes true.

Logical operators:
In addition to the relational operators. C has the following three logical operators.
&& logical AND
|| logical OR
! logical NOT
The logical operators && and || are used when we want to test more than one condition and make decisions.

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 6


An expression of this kind which combines two or more relational expression is termed as a logical expression or
a compound relational expression. Like the simple relational expressions, a logical expression also yields a value
of one or zero (Boolean value), according to the truth table shown below.
Logical AND

Op1 Op2 Op1 && Op2


0 0 0
1 0 0
0 1 0
1 1 1
Logical OR

Op1 Op2 Op1 || Op2


0 0 0
1 0 1
0 1 1
1 1 1

Logical NOT

Op !Op
0 1
1 0

Some examples of the usage of logical expressions are:

1. If(age>55 && salary <1000)


2. If (number<0 || number>100)

Following table shows all the logical operators supported by C language. Assume variable A holds 1 and
variable B holds 0, then −

Operator Description Example

&& Called Logical AND operator. If both the (A && B) is false.


operands are non-zero, then the condition
becomes true.

|| Called Logical OR Operator. If any of the two (A || B) is true.


operands is non-zero, then the condition becomes
true.

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 7


! Called Logical NOT Operator. It is used to !(A && B) is true.
reverse the logical state of its operand. If a
condition is true, then Logical NOT operator will
make it false.

Bitwise Operators:
In arithmetic-logic unit (which is within the CPU), mathematical operations like: addition, subtraction,
multiplication and division are done in bit-level. To perform bit-level operations in C programming, bitwise
operators are used.
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
Bitwise AND operator in C programming.
The output of bitwise AND is 1 if both the corresponding bits of operand is 1. If either of bit is 0 or both bits are
0, the output will be 0. It is a binary operator (works on two operands) and indicated in C programming by &
symbol.
Example: Let us suppose the bitwise AND operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal)
As, every bitwise operator works on each bit of data. The corresponding bits of two inputs are check and if both
bits are 1 then only the output will be 1. In this case, both bits are 1 at only one position,i.e, fourth position from
the right, hence the output bit of that position is 1 and all other bits are 0.
//program to illustrate the use of bitwise AND operator
#include<stdio.h>
void main()
{
int a=12,b=25;
printf("a&b=%d",a&b);
}

Output:
a&b=8

Bitwise OR operator in C
The output of bitwise OR is 1 if either of the bit is 1 or both the bits are 1. In C Programming, bitwise OR
operator is denoted by |.
Example: Let us suppose the bitwise OR operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 8


Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)

//program to illustrate the use of bitwise OR operator


#include<stdio.h>
void main()
{
int a=12,b=25;
printf("a|b=%d",a|b);
}

Output:
a|b=29

C Programming Bitwise XOR(exclusive OR) operator


The output of bitwise XOR operator is 1 if the corresponding bits of two operators are opposite(i.e., To get
corresponding output bit 1; if corresponding bit of first operand is 0 then, corresponding bit of second operand
should be 1 and vice-versa.). It is denoted by ^.
Example: Let us suppose the bitwise XOR operation of two integers 12 and 25.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25


00001100
^ 00011001
________
00010101 = 21 (In decimal)

//program to illustrate the use of bitwise XOR operator


#include<stdio.h>
void main()
{
int a=12,b=25;
printf("a^b=%d",a^b);
}

Output:
a^b=21

Bitwise compliment operator


Bitwise compliment operator is a unary operator (works on one operand only). It changes the corresponding bit of
the operand to opposite bit, i.e., 0 to 1 and 1 to 0. It is denoted by ~.
Example:
35=00100011 (In Binary)

Bitwise complement Operation of 35


~ 00100011
________
Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 9
11011100 = 220 (In decimal)

Twist in bitwise complement operator in C Programming


The bitwise complement of 35 (~35) is -36 instead of 220, but why?
For any integer n, bitwise complement of n will be - (n+1) .

2's Complement
Two's complement is an operation on binary numbers. The 2's complement of a number is equal to the
complement of that number plus 1.

//program to illustrate the use of bitwise complement operator


#include<stdio.h>
void main()
{
int a=35;
printf("~a=%d",~a);
}
Output:
~a=-36

Left Shift Operator


Left shift operator moves the all bits towards the left by certain number of bits which can be specified. It is
denoted by <<.
Example:
Let‘s take a number 14.
Binary representation of 14 is 00001110 (for the sake of clarity let‘s write it using 8 bit)
14 = (00001110)2
Then 14 << 1 will shift the binary sequence 1 position to the left side.

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 10


In the above diagram, you can notice that whenever we shift the number one position to left, the output value will
be exactly number * 2.
If we shift 14 by 1 position to the left, output will be 14 * 2 = 28.
If we shift 14 by 2 position to the left, output will be 14 * 4 = 56.
In general, if we shift a number by n position to left, the output will be number * (2n).
//program to illustrate the use of bitwise left shift operator
#include<stdio.h>
void main()
{
int a=14;
printf("a<<2=%d",a<<2);
}
Output:
a<<2=56
Right Shift Operator
Right shift operator moves the all bits towards the right by certain number of bits which can be specified. It is
denoted by >>.
Example:
Let‘s take a number 14.
Binary representation of 14 is 00001110 (for the sake of clarity let‘s write it using 8 bit)
14 = (00001110) 2
Then 14 >> 1 will shift the binary sequence by 1 position to the right side.

In the above diagram, you can notice that whenever we shift the number one position to right, the output value
will be exactly number / 2.
If I shift 14 by 1 position to the right, output will be 14 / 2 = 7. i.e 14/2 = 7
Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 11
If I shift 14 by 2 position to the right, output will be 14 / 4 = 3. i.e 14/4 =3.5 since it‘s an integer, fractional part
will not be considered.
In general, if we shift a number by n times to right, the output will be number / (2n).
//program to illustrate the use of bitwise right shift operator
#include<stdio.h>
void main()
{
int a=14;
printf("a>>2=%d",a>>2);
}
Output:
a>>2=3

The conditional operators (Ternary Operator):


An operator called ternary operator or conditional operator represents pair ―? :‖ which is available in C to
construct conditional expressions of the form.

expression1? expression2: expression3;

The ternary operator take three arguments:


1. The first is a comparison argument
2. The second is the result upon a true comparison
3. The third is the result upon a false comparison

The operator ? : works as follows: expression1 is evaluated first. If it is nonzero (true), then the expression2 is
evaluated and becomes the value of the expression. If expression1 is false, expression3 is evaluated and its value
becomes the value of the expression. Note that only one of the expressions (either expression2 or expression3) is
evaluated.

The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-
else statement but the conditional operator takes less space and helps to write the if-else statements in the
shortest way possible.
For example, consider the following statements
x=3;
y=15;
z=x>y?x:y;
In this example, z will be assigned the value of y. This can be achieved using the if..else statements as follows:
if (x>y)
Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 12
z=x;
else
z=y;
Example:
/*program to find smallest of two given numbers using conditional operator*/
#include<stdio.h>
void main( )
{
int a,b,s;
printf("enter two numbers");
scanf("%d%d",&a,&b);
s=a<b?a:b;
printf("smallest number is %d",s);
}

Output:
enter two numbers
53
smallest number is 3

Special Operators:
Comma Operator:
Comma operators are used to link related expressions together.
For example: int a,c=5,d;

The sizeof Operator:


The sizeof operator tells us the size, in bytes, of a type or a primary expression. By specifying the size of
an object during execution, we make our programmer e portable to other hardware. A simple example will
illustrate the point. On some personal computers, the size of the integer type is 2 bytes. On some mainframe
computers, it is 4 bytes. On the very large super computers, it can be as large as 16 bytes. If it is important to
know the exact size (in bytes) of an integer, we can use the sizeof operator with the integer type as shown below:
sizeof(int)
It is also possible to find the size of a primary expression. Here are two examples:
sizeof -345.23 sizeof x

Expression:
An expression is a combination of operators, constants and variables. An expression may consist of one or
more operands, and zero or more operators to produce a value.

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 13


Arithmetic expressions:
An arithmetic expression is a combination of variables, constants and operators arranged as per the syntax of the
language.
Expressions are evaluated using an assignment statement of the form
Variable=expression;
The table below shows the algebraic expression and C language expression
Algebraic expression C expression
a x b-c a*b-c
(m + n) (x + y) (m + n) *(x + y)
ab/c a*b/c
3x2+2x+1 3*x*x+2*x+1
x/y +c x/y + c
Variable is any valid C variable name. When the statement is encountered, the expression is evaluated first
and the result then replaces the precious value of the variable on the left-hand side. All variables used in the
expression must be assigned values before evaluation is attempted.
x=a*b-c;
y=b/c*a;
z=a-b/c+d;
The blank space around an operator is optional and adds only to improve readability. When these statements
are used in a program, the variables a, b, c and d must be defined before they are used in the expressions.

Examples for conversion of mathematical expressions into C equivalent expressions:

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 14


Modes of expression:
There are three different modes of expression.
1. Integer Arithmetic
2. Real Arithmetic
3. Mixed-mode Arithmetic
Integer Arithmetic
When both the operands in a single arithmetic expression such as a+b are integers, the expression
is called an integer expression, and the operation is called integer arithmetic. This mode of expression always
yields an integer value. The largest integer value depends on the machine, as pointed out earlier
Example:

If a and b are integers then for a=14 and b=4


We have the following results:
a - b=10
a + b = 18
a * b = 56
a / b=3
Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 15
a %b=2
During integer division, if both the operands are of the same sign, the result is truncated towards zero. If one of
them is negative, the direction of truncation is implementation dependent. That is,
6/7=0 and -6/-7=0

but -6/7 may be zero -1 (Machine dependent)

Similarly, during modulo division , the sign of the result is always the sign of the first operand(the dividend)
That is
-14 % 3 =-2
-14 % -3= -2
14 % -3=2
Real Arithmetic
An arithmetic operation involving only real operands is called real arithmetic. A real operand may assume
values either in decimal or exponential notation. Since floating point values are rounded to the number of
significant digits permissible, the final value is an approximation of the correct result. If x, y, and z are floats, then
we will have:
x=6.0/7.0=0.857143
y= 1.0/3.0 =0.333333
z= -2.0/3.0= -0.666667

The operator % cannot be used with real operands.

Mixed- mode Arithmetic


When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic
expression. If either operand is of the real type, then only the real operation is performed and the result is always a
real number.

Thus
15/10.0=1.5
where as
15/10=1
Precedence and Associativity:
Precedence is used to determine the order in which different operators in a complex expression are
evaluated. Associativity is used to determine the order in which operators with the same precedence are evaluated
in a complex expression. Another way of stating this is that associativity determines how operators with the same
precedence are grouped together to form complex expression. Precedence is applied before associativity to
determine the order in which expressions are evaluated. Associativity is then applied, if necessary.

Precedence of arithmetic operator:


An arithmetic expression without parenthesis will be evaluated from left to right using the rule of precedence of
operators.
There are two distinct priority levels of arithmetic operators in C:
High priority: *, /, %
Low priority: +, -
The basic evaluation procedure includes ‗two left to right‘ passes through expression:
1. During the first phase high priority operators (if any) are applied as they are encountered.
2. During the second phase, the low priority operators (if any) are applied as they are encountered.
Example:
x=a-b/3+c*2-1 where a=9, b=12, and c=3 then
x=9-12/3+3*2-1
Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 16
First pass:
Step 1: x=9-4+3*2-1
Step 2: x=9-4+6-1
Second pass:
Step 3: x=5+6-1
Step 4: x=11-1
Step 5: x=10
These steps are illustrated in the following figure:

Use of parentheses:

Parentheses are used if the order of operations governed by the precedence rules are to overridden.
In the expression with a single pair of parentheses the expression inside the parentheses is evaluated FIRST.
Within the parentheses the evaluation is governed by the precedence rules.
For example, in the expression:
a * b/(c+d * k/m+k)+a
the expression within the parentheses is evaluated first giving:
c+dk/m+k
After this the expression is evaluated from left to right using again the rules of precedence giving
ab/c+dk/m+k +a

If an expression has many pairs of parentheses then the expression in the innermost pair is evaluated first, the
next innermost and so on till all parentheses are removed. After this the operator precedence rules are used in
evaluating the rest of the expression.
((x * y)+z/(n*p+j)+x)/y+z
xy,np+j will be evaluated first.
In the next scan
xy+z/np+j +x
Will be evaluated. In the final scan the expression evaluated would be:
(xy+ z/np+j+x)/y +z
Associativity:
Associativity can be left-to-right or right-to-left. Left-to-right associativity evaluates the expression by
starting on the left and moving to the right. Conversely, right-to-left associativity evaluates the expression by
proceeding from the right to the left.
Left-to-right Associativity:
The following shows and example of left-to-right associativity. Here we have four operators of the same
precedence (* / % *)
3 * 8 / 4 % 4 *5
Associativity determines how the subexpressions are grouped together. All of these operators have the
same precedence. Their associativity is from left to right. So they are grouped as follows:

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 17


( ( ( ( 3 * 8) / 4) * 5)
The value of this expression is 10.
Right-to-left Associativity:
Several operators have right-to-left associativity. For example, when more than one assignment operator
occurs in an assignment expression, the assignment operators must be interpreted from right to left. This means
that the rightmost expression will be evaluated first; then its value will be assigned to the operand on the left of
the assignment operator and the next expression will be evaluated. Under these rules, the expression a+=b*=c-=5
is evaluated as,
(a += (b *= (c -= (5 ) ) )
Which is expanded to (a =a + (b = b * (c - = 5 ) ) )
Side Effects:
A side effect is an action that results from the evaluation of an expression. For example, in an assignment,
C first evaluates the expression on the right of the assignment operator and then places the value in the left
variable. Changing the value of the left variable is a side effect. Consider the following expression: x=4;
This simple expression has three parts. First, on the right of the assignment operator is primary expression
that has the value 4. Second, the whole expression x=4 also has a value of 4. And third, as a side effect, x receives
the value 4.
Relational and logical expressions:
We have seen that float or integer quantities may be connected by relational operators to yield an answer which is
true of false.
For example the expression, Marks>=60
Would have an answer true if marks is greater than or equal to 60 and false if marks is less than 60. The result of
the comparison (marks>=60) is called a logical quantity. C provides a facility to combine such logical quantities
by logical operators to logical expressions. These logical expressions are useful in translating intricate problem
statements.
Precedence of relational operators and logical operators:
Example: (a>b *5) &&(x<y+6)
In the above example, the expressions within the parentheses are evaluated first. The arithmetic operations are
carried out before the relational operations. Thus b*5 is calculated and after that a is compared with it. Similarly
y+6 is evaluated first and then x is compared with it .
Precedence of C operators
Operator Description Associativity Rank
() Function Call Left to Right 1
[] Array element reference
+ Unary plus 2
- Unary minus
++ Increment
-- Decrement Right to Left
! Logical NOT
~ Bitwise Complement
* Pointer reference (indirection)
& Address
sizeof Size of an object
(type) Type Cast (conversion)
* Multiplication Left to Right 3
/ Division
% Modulus
+ Addition Left to Right 4
- Subtraction
<< Left Shift Left to Right 5
>> Right Shift
< Less than Left to Right 6
<= Less than or equal to

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 18


> Greater than
>= Greater than or equal to
== Equality Left to Right 7
!= Inequality
& Bitwise AND Left to Right 8
^ Bitwise XOR Left to Right 9
| Bitwise OR Left to Right 10
&& Logical AND Left to Right 11
|| Logical OR Left to Right 12
?: Conditional Expression Right to Left 13
=, *=, /=,%=, Assignment Operators Right to Left 14
+=,
-=,&=, ^=, |=,
<<=, >>=
, Comma operator Left to Right 15

Assignment expressions and assignment statements:


The general syntax for an assignment statement is: v op=exp;
Where v is a variable, exp is an expression and op is a C binary arithmetic operator. The operator op=is known as
the shorthand assignment operator.
The assignment statement
v op=exp;
is equivalent to
v=v op(exp);
with v evaluated only once. Consider the following example:
a+=b+1;
This is same as the statement
a=a+(b+1);
The shorthand operator += means ‗add b+1 to a‘ or ‗increment a by b+1‘ . For b=2, the above statement becomes
a+=3;
and when this statement is executed, 3 is added to a, if the old value of a is 4 then the new value of a will be 7.
Consider the following expression, evaluate the equivalent expression and their final result.
int i, j, k, m, n ;
i=j=2;
Expression Equivalent expression value
i+=++j+3 i=(i+((++j)+3) ) 8
When an assignment statement is executed, the expression on the right of the assignment operator is first
evaluated and the number obtained is stored in the storage location named by the variable name appearing on the
left of the assignment operator.

Type Conversion:
Implicit Type Conversion:
When the types of the two operands in a binary expression are different, C automatically converts
one type to another. This is known as implicit type conversion.
Example: int i =1234;
float d;
d=i; // value of d is 1234.0
Explicit Type Conversion:
C allows mixing of float with integers in expressions. In such cases integers are converted to float
before computation. C provides explicit type conversion functions using which a programmer can intentionally
change the type of expressions in an arithmetic statement. This is done by a unary operator called a cast. If we
write (type_name) expression the expression is converted to the type_name
Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 19
For example
(float)(integer expression or variable name)
then the integer expression or variable name is converted to float. If we write:
(int)(float expression or variable name)
then the float expression or variable name is converted to integer.

X = (int) 2.3 ;
Y = (float) 234;

Conversion with float and double:


If an expression real variables declared as float and double appear together all variables are
converted to double.
For example, in the statement:
float x y ,s;
double p ,q ,z;
x=z/(y + s);
the variable names y and s are assumed to be double. The result is double as z is double.

Two-way Selection:
The basic decision statement in the computer is the two-way selection. The decision is
described to the computers as a conditional statement that can be answered either true or false. If
the answer is true, one or more action statements are executed. If the answer is false, then a
different action or set of actions is executed. Regardless of which set of actions is executed, the
program continues with the next statement after the selection.
The C has two different Two-way selections, the if statement, the if-else statement

if Statement:
The general form of if statement looks like

if ( Boolean condition) or if(Boolean condition)


Statement; {
Statement 1;
Statement 2;
:
Statement n;
}

The keyword if tells the compiler if the condition is true, then statement should be executed.
Suppose if the condition is false, then statement has to be skipped.

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 20


The flowchart for if statement is shown below:

Boolean False
Condition

True

Statements

In the preceding syntaxes, a simple or compound statement (block of statements) is executed when
the condition expression given in the if statement is turn out to be true. Otherwise, the program
control passes to the next statement. If the condition expression is false then the C compiler does
not do anything. Note that the condition expression given in parentheses, must be evaluated as true
(non-zero value) or false (zero value). In addition, a compound statement must be provided by
opening and closing braces.
Example: Given two integer numbers, find out the larger of them.
#include <stdio.h>
main()
{
float x, y;
printf(― input two numbers\n‖);
scanf(―%f %f ―, &x, &y);
if ( x > y)
printf(― x is big\n‖);
if (y > x)
printf(―y is big\n‖);
}

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 21


if-else statement:
The if statement by itself will execute a single statement or a group of statements, when
the condition following if is true. It does nothing when it is false. The if-else statement provides
the option a statement/s can be executed when the condition is false.
The syntax is

if (Boolean condition) or if(Boolean condition)


Statement 1; {
else Statement 1;
Statement 2; Statement 2;
:
Statement n;
}
else
{
Statement 1;
Statement 2;
:
Statement n;
}

If the condition is true, the statement1 is executed. If the condition is false, then statement2 under
else part is get executed.
The if-else statement executes a simple or compound statement when the conditional
expression provided in the if statement is true. It executes another simple or compound statement,
followed by the else statement, when the conditional expression is false. The flowchart for the if-
else is shown below:

False Boolean True


Condition

Statement2 Statement1

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 22


Let us consider an example.

Problem : : Find out a number is even or odd

#include <stdio.h>
main()
{
int x;
printf(―input an integer\n‖);
scanf(―%d‖,&x);
if ((x % 2) == 0)
printf(―it is even number\n‖);
else
printf(―it is odd number\n‖);
}
Compound Statements:
A compound statement is a unit of code consisting of zero or more statements. It is
also known as block. A compound statement consists of an opening brace, an optional declaration
and definition section and an optional statement section, followed by a closing brace.

Example: if(logical condition)


{
Statement1;
Statement 2;
.
Statement n;
}

Nested if Statements:
The term nested if statements means one if statement contains another if statement. The
control of a program moves into the inner if statement when the outer if statement is evaluated to
be true. When an if…else is included within an if… else, it is known as a nested if statement. The
following flowchart illustrates the nested if statement. There is no limit to how many levels can be
nested, but if there are more than three, they can become difficult to read.

The general syntax for the above nested if statement is:

if (Boolean condition 1)
if(Boolean condition 2)
Statement 1;
else
Statement 2;
else
Statement 3;

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 23


Boolean
False Condition1 True

Statement3 Boolean
True False
Condition2

Statement1 Statement2

Example: The following program illustrates the nested if in two-way selection.


#include<stdio.h>
main( )
{
int a,b;
printf(―enter two integers‖);
scanf(―%d%d‖,&a,&b);
if(a<=b)
if(a<b)
printf(―%d < %d\n‖,a,b);
else
printf(―%d == %d\n‖,a,b);
else
printf(―%d > %d‖, a,b);
}

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 24


Dangling else Problem:
Once we start nesting if-else statements, however, we encounter a classic problem known
as the dangling else. This problem is created when there is no matching else for every if. Solution
to this problem is a simple rule: always pair an else to the most recent unpaired if in the current
block. In the following code, dangling else problem is shown.

if (Boolean condition 1)
if(Boolean condition 2)
Statement 1;
else /* dangling else */
Statement 2;
The following code is the solution to the dangling else problem.
if (Boolean condition 1)
{
if(Boolean condition 2)
Statement 1;
}
else /* dangling else */
Statement 2;

The if-else-if Ladder:


Sometimes we wish to make a multi-way decision based on several conditions. The most
general way of doing this is by using the else if variant on the if statement. This works by cascading
several comparisons. As soon as one of these gives a true result, the following statement or block
is executed, and no further comparisons are performed. If none of the conditions is true, the final
else is executed. The final else statement is optional. If final else is not present, no action takes
place if all other conditions are false.

The syntax of if-else-if ladder is as follows:

if (Boolean condition)
Statement ;
else if (Boolean condition)
Statement ;
else if (Boolean condition)
Statement ;
else if (Boolean condition)
Statement ;
else
Statement ;

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 25


It is up to the programmer to devise the correct structure for each programming problem.
Example:
// Demonstration of if-else-if ladder

#include <stdio.h>
main ( )
{
int x;
printf("Enter an integer between 1 and 6");
scanf(―%d‖,&x);
if(x==1)
printf("The number is one \n");
else if(x==2)
printf("The number is two \n");
else if(x==3)
printf("The number is three\n");
else if(x==4)
printf("The number is four \n");
else if(x==5)
printf("The number is five \n");
else if(x==6)
printf("The number is six \n");
else
printf("You didn't follow the rules");
}
Multi-way Selection:
In addition to two-way selection, most programming languages provide another selection
concept known as multi-way selection.
Switch statement:

The C switch allows multiple choice of a selection of items at one level of a conditional where it
is a far neater way of writing multiple if statements:

This is another form of the multi way decision. It is well structured, but can only be used in certain
cases where;

 Only one variable is tested, all branches must depend on the value of that variable. The
variable must be an integral type. (int, long, short or char).
 Each possible value of the variable can control a single branch. A final, catch all, default
branch may optionally be used to trap all unspecified cases.

A switch statement is a conditional statement that tests a value against different values. If the
value is matched, the corresponding group of statements is executed. A switch statement begins
with the switch keyword that is followed by a value expression in the parenthesis ( ). It is a
combination of multiple case labels that must be separated by the break statement. Every case
label contains a constant value that is matched against the value, which is specified in the

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 26


switch expression. If the value is matched, the statements of that case label are executed. In
addition, we can specify the default label, which is executed when the value specified in the
switch expression, does not match with the given case labels. The general syntax for switch
statement is:

switch (expression) {
case item1:
statement1;
break;
case item2:
statement2;
break;

case itemn:
case itemn statementn;
break;
default:
statement;
break;
}

In each case the value of itemi must be a constant, variables are not allowed. Floating point
constants are not allowed to represent case value.

The break is needed if you want to terminate the switch after execution of one choice.
Otherwise the next case would get evaluated.

The default case is optional and catches any other cases.

For example: - switch (letter)

{
case `A':
case `E':
case `I':
case `O':
case `U':
numberofvowels++;
break;

case ‘ ':
numberofspaces++;
break;

default:
numberofconsonants++;
break;
}

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 27


In the above example if the value of letter is `A', `E', `I', `O' or `U' then
numberofvowels is incremented.

If the value of letter is ` ' then numberofspaces is incremented.

If none of these is true then the default condition is executed, that is


numberofconsonants is incremented.

The general flowchart of the switch statement is shown below:

Multi-way
Expression

Value1 Value2 Value3 Value4

Statement1 Statement2 Statement3 Statement4

Iterative Statements (Looping Statements):

The term iteration means repetitive execution of the same set of instructions for a given
number of times or until a specified result is obtained. The statements that are executed repetitively
are called as iterative statements. Iterative statements are also popularly known as loop constructs,
which repeatedly execute a block of code based on a condition expression, as long as the condition
evaluates to true. The loop exits when the conditional expression evaluates to false. After that, the
control transferred to the next statement that follows the loop. The following are the iterative
statements supported by C:

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 28


 The while loop
 The do-while loop
 The for loop

Loop statement in C:

C language provides three types of loop, while, do while and for.

 The while loop keeps repeating an action until an associated test returns false. This is
useful where the programmer does not know in advance how many times the loop will be
traversed.
 The do while loops is similar, but the test occurs after the loop body is executed. This
ensures that the loop body is run at least once.
 The for loop is frequently used, usually where the loop will be traversed a fixed number
of times. It is very flexible, and novice programmers should take care not to abuse the
power it offers.

while statement:

We use a while statement to continually execute a block of statements while a condition remains
true. The following is the general syntax of the while statement.

while (expression)
{
Statement 1;
Statement 2;
:
Statement n;
}

First, the while statement evaluates expression, which must return a Boolean value. If the
expression returns true, the while statement executes the statement(s) in the while block. The
while statement continues testing the expression and executing its block until the expression
returns false. In while loop, first conditional expression is evaluated and then statements are
executed if the expression returns true. Therefore while loop is also called as entry condition loop.

Let us consider a simple example, to calculate the factorial of a number

#include <stdio.h>
main()
{
int fact=1, i=1,n;
printf(―input a number\n‖);
scanf(―%d‖,&n);
if(n==0)
fact=1;
else

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 29


{
while ( i <= n)
{
fact=fact*i;
++i;
}
}
printf(― factorial = %d\n‖, fact);
}

The general flow chart of the while loop is shown below:

Loop False
Condition

True

Statements

do –while statement:

The general syntax of the do-while loop is:


do
{
Statement 1;
Statement 2;
:
Statement n;
}
while (expression);

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 30


Instead of evaluating the expression at the top of the loop, do-while evaluates the
expression at the bottom. Thus, the statements within the block associated with a do-while are
executed at least once.

Each line of a C program up to the semicolon is called a statement. The semicolon is the statement's
terminator. The braces { and } which have appeared at the beginning and end of our program unit
can also be used to group together related declarations and statements into a compound statement
or a block.

In the case of the while loop before the compound statement is carried out the condition is
checked, and if it is true the statement is obeyed one more time. If the condition turns out to be
false, the looping isn't obeyed and the program moves on to the next statement. So we can see that
the instruction really means while something or other is true keep on doing the statement.

In the case of the do while loop it will always execute the code within the loop at least once, since
the condition controlling the loop is tested at the bottom of the loop. The do while loop repeats
the instruction while the condition is true. If the condition turns out to be false, the looping isn't
obeyed and the program moves on to the next statement. Therefore, do-while loop is also called as
exit-condition loop.

Let us consider an example to calculate the factorial using do-while loop.


#include <stdio.h>
main()
{
int fact=1, i=1,n;
printf(―input a number\n‖);
scanf(―%d‖,&n);
if(n==0)
fact=1;
else
{
do
{
fact=fact*i;
++i;
}
while ( i <= n);
}
printf(― factorial = %d\n‖, fact);
}

The general flowchart of the do-while loop is shown below:

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 31


Statements

Loop
True Condition

False

for statement:
The for statement provides a compact way to iterate over a range of values. The general
form of the for statement can be expressed as follows.

for (initialization; loop_condition; loop_progress_expression)


{

Statement 1;
Statement 2;
:
Statement n;
}

The initialization expression initializes the loop — it's executed once at the beginning of the loop.
The loop_condition determines when to terminate the loop. When the expression evaluates to
false, the loop terminates. Finally, loop_progress_expression is an expression that gets invoked
after each iteration through the loop. All these components are optional. In fact, to write an infinite
loop, you omit all three expressions.
for ( ; ; ) { //infinite loop
...
}

Let us consider an example to calculate the factorial of a number using the for loop.

#include <stdio.h>
main()
{

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 32


int n,fact=1,i;
printf(―enter an integer\n‖);
scanf(―%d‖,&n);
if(n==0)
fact=1;
else
{
for (i=1; i<=n; ++i)
fact=fact*i;
}
printf(―factorial = %d\n‖, fact);
}

The general flowchart of the for loop is shown below:

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 33


Pretest and Post-test loops:
Programming languages allow us to check the loop control expression either before or after
each iteration of the loop. In other words, we can have either a pre or a post-test terminating
condition. In a pretest loop, the condition is checked before we start and at the beginning of each
iteration. If the test condition is true, we execute the code; if the test condition is false, we terminate
the loop. Example for pretest loop is the while loop.
In the post-test loop, we always execute the action at least once. The loop control
expression is then tested. If the expression is true, the loop repeats; if the expression is false, the
loop terminates. Example for post-test loop is the do-while loop.

The Infinite Loop:


A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used
for this purpose. Since none of the three expressions that form the for loop are required, you can
make an endless loop by leaving the conditional expression empty.
#include <stdio.h>
main ()
{
for( ; ; )
printf("This loop will run forever.\n");
}
When the conditional expression is absent, it is assumed to be true. You may have an initialization
and increment expression, but C programmers more commonly use the for(;;) construct to signify
an infinite loop.
The Comma Expression:
A comma expression is a complex expression made up of two expressions separated by a
comma. Although it can legally e used in many places, it is most often used in for statement. The
expressions are evaluated left to right. The value and type of the expressions are the value and type
of the right expression; the left expression is included only for its side effect. The comma
expression has the lowest priority of all expressions.
The following statement uses a comma expression to initialize the variable sum and i in the loop.
for(sum=0,i=1; i<=20; i++)
sum=sum+i;
Comma expression can be nested. When they are nested, all expression values other than the last
are discarded. The format of a nested comma expression is shown below:

expression, expression , expression

If we use a comma expression for the second expression in a for loop, make sure that the loop
control is the last expression.

Nested Loop:
The loop can be nested (i.e. embedded) one within another is called nested loop. The same
type of control structure need not generate the inner loop and outer loop. It is essential, however,

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 34


that one loop be completely embedded within the other- there can be no overlap. Also, each loop
must be controlled by a different index.
When a for loop contained another for loop, it is called a nested for loop. In the nested for
loop, execution starts from the outer for loop and the control passes to the inner for loop. The inner
for loop then continues execution till the condition of inner for loop evaluates to true. When the
inner for loop is completed, the flow of execution is passed to the outer for loop to continue the
next iteration and then again the inner for loop is executed. This implies that for the each iteration
of the outer loop, the inner loop is executed completely.

The following program illustrates the concept of nested loop.


#include<stdio.h>
main( )
{
int x, y;
for(x=1;x<=4;x++)
{
for(y=1;y<=x;y++)
printf(―%d\t‖,y);
printf(―\n‖);
}
}
Output:
1
1 2
1 2 3
1 2 3 4

Other Statements Related to Looping:


Two other C statements related to loops are break and continue.
break statement:
The break statement is used to break any type of loop as well as switch statement. Breaking
a loop means terminating the loop. In a loop, the break statement causes a loop to terminate. It is
the same as setting the loop‘s limit test to false. If we are in a series of nested loops, break
terminates only the inner loop- the one we are currently in. Note that break statement needs a
semicolon. The syntax of the break statement is as follows:

break;

The break statement can be used in any of the loop statements- while, for and do-while and
in the selection switch statement. The following example illustrates the use of break statement in
a loop.
#include<stdio.h>
main( )
{

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 35


int i=0;
while(1)
{
i=i+2;
if(i>10)
break;
printf(―%d\n‖,i);
}
}
Output:
2
4
6
8
10

In the above program, the variable i is initialized with zero value and the while loop is used to
display even numbers from 2 to infinity. However, the program displays even numbers up to 10.
This is because, an if statement checks whether the number stored in the variable i is greater than
10; and if it is greater than 10, the while loop is terminated by the break statement.

continue statement:
The break statement breaks the entire loop, but a continue statement breaks the current
iteration. In other words, the continue statement breaks the current execution of a loop condition
and then continue the loop with next condition. However, the break statement breaks the entire
loop when a specified condition of the loop is met. Note the continue statement also needs a
semicolon. The syntax of the continue statement is as follows:

continue;

The following program illustrates the use of continue statement.


#include<stdio.h>
main( )
{
int i;
for(i=2;i<=20;i=i+2)
{
if(i == 6 | | i == 14)
continue;
printf(―%d\n‖,i);
}

Output:
2
4
8

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 36


10
12
16
18
20

In the above program, first 10 even numbers are printed excluding 6 and 14. This is done with the
help of the continue statement inside an if condition, which gets executed in case the value of I
variable becomes either 6 or 14. In the output of the above program, even numbers are shown
successfully until the value of i becomes 6 or 14. When the value of i becomes 6, the control of
execution quits from the current loop and returns back to the for loop. The next iteration then
displays the remaining even numbers till the value of i becomes 14. Then, the control of execution
again quits the current loop and returns back to the for loop. The next iteration displays the
remaining even numbers.
Unconditional Control Statement- goto Statement:
The goto statement is used to perform a transfer of control from one statement to another
in a program. This statement uses user-defined labels to specify the goto statement in the program.
The syntax to use the goto statement is

goto label_name;
………….
………….
label_name: Statement;

In the preceding syntax, label_name specifies a user-defined label for the target statement
to which the control should be transferred. This label should be followed by a colon and the target
statement must be preceded by the label.
goto statement is not allowed in structured programming.

Example: goto xyz;


…………..
……………
xyz: printf(―divided by zero error\n‖);
printf(―executing is halted\n‖);
…………
This statement informs the computer to jump or to transfer unconditionally to that part of
the program beginning with statement labeled xyz.
Sometimes goto statement is coupled with conditional branching statement, for example
the if control structure.
Example: i=1;
pqr: printf(―%d‖,i)
i++;
if(i<=10)
goto pqr;

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 37


Review Questions
1. Describe various types of operators supported by C with examples. (10 Marks)
2. Describe the arithmetic operators in C. List the rules associated with their use. (6 Marks)
3. Explain precedence and associativity of arithmetic operators with examples. (4 Marks)
4. List out and explain the relational operators in C with examples. (5 Marks)
5. Describe the logical operators in C. (4 Marks)
6. State the important points regarding assignment operator. (4 Marks)
7. What is unary operator? Explain various unary operators supported by C.
8. Explain the conditional operators in C with an example. (5 Marks)
9. What are bitwise operators? List them and explain their purpose. (6 Marks)
10. What is type conversion? Explain type conversion with example. (5 Marks)
11. Write a C program to perform all arithmetic operations for the given two integers. (5 Marks)
12. What is the purpose of if-else statement? Give its syntax. (4 Marks)
13. Describe the different forms of if-else statement. How they differ? (6 Marks)
14. What is nested if statement? Explain its syntax with example. (5 Marks)
15. Explain if and if-else statement with syntax. Give examples for each. (6 Marks)
16. Distinguish between if and if-else statement. (3 Marks)
17. Explain the dangling else problem. How can it be solved? (4 Marks)
18. Explain if-else-if ladder with example. (5 Marks)
19. What is the purpose of switch statement? (4 Marks)
20. Explain switch statement with syntax. (5 Marks)
21. What are case label? What type of expression must be used to represent a case label? (4
Marks)
22. What is the purpose of break statement in switch? (2 Marks)
23. Distinguish between switch and if-else statement. (3 Marks)
24. Write the syntax of different branching statements and explain their working. (10 Marks)
25. What is the purpose of goto statement? Why we should avoid the use of goto statement?
(4 Marks)
26. What is meant by looping? Explain the different types of loops in ‗C‘. (10 Marks)
27. Explain while loop with syntax. (5 Marks)
28. Explain do-while loop with syntax. (5 Marks)
29. Explain for statement with syntax. (5 Marks)
30. What is nested loop? Explain with example. (5 Marks)
31. Distinguish between while and do-while statement. (4 Marks)
32. Explain the use of break and continue statement in loop with example. (6 Marks)
33. What is the use of comma operator in for loop? Explain with example. (4 Marks)
34. What is infinite loop? Explain with example. (4 Marks)
35. Write a ‗C‘ program to find sum of the given digits. (6 Marks)
36. Write a ‗C‘ program to find the sum of N natural numbers using for loop. (4 Marks)
37. Write a ‗C‘ program to find factorial of the given number. (6 Marks)
38. Write a ‗C‘ program to check whether given number is palindrome or not. (6 Marks)
39. Write a ‗C‘ program to find sum of numbers from 1 to n. (5 Marks)
40. Write a ‗C‘ program to find roots of quadratic equation. (6 Marks)
41. Write a ‗C‘ program to check the given number is odd or even. (4 Marks)
42. Write a ‗C‘ program to check the given number is prime or not. (6 Marks)
43. Write a ‗C‘ program to perform all arithmetic operations for the given two integers using
switch statement.

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 38


OR
Write a C program to implement commercial calculator using switch statement. (6 Marks)
44. Write a ‗C‘ program to plot Pascal‘s triangle. (8 Marks)
45. Write a ‗C‘ program to print the following output using for loops. (4 Marks)
1
22
333
4444
55555

Notes Prepared by Sayeesh, Dept. of CSE, YIT Page 39

You might also like