Writeup Points For Lab Assignment 2,3
Writeup Points For Lab Assignment 2,3
Assignment No.2
Problem Statement: To learn different operators in C and use them in program by developing
program
1. Write a Program in C to perform basic arithmetic operation on two numbers.
2. Write a program in C to calculate Simple Interest
3. Write a Program in C Calculate Area of Circle
Pre-requisite: Turbo C, basic header files, IO statements in C, Operators.
Theory:
Data types in C.
Arithmetic operators
The operators which are used to perform arithmetic/mathematical operations on operands there
are 9 arithmetic operators in c.
-1-
Department of
Subtracts right
– Minus operand from left a–b
2 operand.
Multiply two
* Multiply a*b
3 numeric values.
Divide two
/ Divide a/b
4 numeric values.
Returns the
remainder after
% Modulus diving the left a%b
operand with the
5 right operand.
Used to specify
+ Unary Plus the positive +a
6 values.
Increases the
++ Increment value of the a++
8 operand by 1.
Decreases the
— Decrement value of the a–
9 operand by 1.
-2-
Department of
Relational operators
The relational operators in C are used for the comparison of the two operands. All these
operators are binary operators that return true or false values as the result of comparison.
These are a total of 6 relational operators in C
Returns true if
== Equal to both the operands a == b
5 are equal.
Returns true if
Not equal to both the operands a != b
!=
are NOT equal.
6
-3-
Department of
Logical operators
Logical Operators are used to combine two or more conditions/constraints or to complement the
evaluation of the original condition in consideration. The result of the operation of a logical
operator is a Boolean value either true or false.
Returns true if
&& Logical AND both the operands a && b
1 are true.
Returns true if
|| Logical OR both or any of the a || b
2 operand is true.
Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the operands. The operators
are first converted to bit-level and then the calculation is performed on the operands.
Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at
the bit level for faster processing. There are 6 bitwise operators in C
S. No. Symbol Operator Description Syntax
Performs bit-by-
bit AND operation
& Bitwise AND a&b
and returns the
1 result.
Performs bit-by-
bit OR operation
| Bitwise OR a|b
and returns the
2 result.
-4-
Department of
Performs bit-by-
bit XOR operation
^ Bitwise XOR a^b
and returns the
3 result.
Assignment Operators in C
Assignment operators are used to assign value to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment operator is a
value. The value on the right side must be of the same data type as the variable on the left side
otherwise the compiler will raise an error.
The assignment operators can be combined with some other operators in C to provide multiple
operations using single operator. These operators are called compound operators.
In C, there are 11 assignment operators :
-5-
Department of
left operand.
Assign the
remainder in the
Modulus and division of left
%= a %= b
assign operand with the
right operand to
6 the left operand.
Performs bitwise
AND and assigns
&= AND and assign a &= b
this value to the
left operand.
7
Performs bitwise
OR and assigns
|= OR and assign a |= b
this value to the
left operand.
8
-6-
Department of
Performs bitwise
Rightshift and Rightshift and
>>= assign assign this value to a >>= b
the left operand.
10
Performs bitwise
Leftshift and Leftshift and
11 <<= assign assign this value to a <<= b
the left operand.
Conditional Operator ( ? : )
The conditional operator is the only ternary operator in C+.
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then we
will execute and return the result of Expression2 otherwise if the condition(Expression1)
is false then we will execute and return the result of Expression3.
Syntax
and
structure_pointer -> member;
sizeof Operator
sizeof is much used in the C programming language.
-7-
Department of
It is a compile-time unary operator which can be used to compute the size of its operand.
The result of sizeof is of the unsigned integral type which is usually denoted by size_t.
Basically, the sizeof the operator is used to compute the size of the variable or datatype.
Syntax
sizeof (operand)
Expressions in C
Expressions are the combination of variables, operands, and operators. The result
would be stored in the variable once the expressions are processed based on the operator's
precedence.
Arithmetic Expression
The arithmetic expression is evaluated in specific order considering the operator's precedence, and
the result of an expression will be based on the type of variable.
Example:
a =2 b=3 c=4
z=a+b-(a*c);
z= 2 + 3 - (2 * 4)
flow of execution
z=2+3-(2*4)
z=2+3-8
-8-
Department of
z=5-8
z=-3
so result is z=-3
Relational Expressions:
Relational operators >, <, ==,!= etc are used to compare 2 operands. Relational expressions
consisting of operands, variables, operators, and the result after evaluation would be either true or
false.
Example:
C=a>b
a*b == a+b
z=a*b>a+c
Flow of execution
z=3*2>3+1
z=6>4
Output is z=1
Logical Expressions:
Relational expressions and arithmetic expressions are connected with the logical operators, and the
result after an evaluation is stored in the variable, which is either true or false.
Example:
a>b || b>a
-9-
Department of
From the given below logical expression example, let's consider the values of operands to
be
z=(a+b)>c&&a<b
Flow of executions
Z=(2+4)>3&&2<4
Z=6>3&&2<4
Z=T&&T
Z=true
Z=1
Conditional Expressions:
The general syntax of conditional expression is:
From the given above expressions, the first expression (exp1) is conditional, and if the condition is
satisfied, then expression2 will be executed; otherwise, expression3 will be performed.
Example:
2<3? 2 : 3
From the given below conditional expressions example, let's consider the values of operands to be
a=5, b=3, and c=1.
- 10 -
Department of
Below given steps are in order of precedence in which the operators of an expression must be
evaluated.
Algorithm:
1 algorithm of program to perform basic arithmetic operation on two numbers.
2 algorithm of program to calculate Simple Interest
3 algorithm of program to Calculate Area of Circle
Flow Chart
1
Addition is:- 40
Subtraction is:-20
Multiplication is:-300
Division is:-3
Modulus is :-0
- 11 -
Department of
Area of Circle is
- 12 -
Department of
Assignment No.3
Aim:-Study Decision making statements in C
Problem statement:
1. Write a program in c to accept three numbers from user and find the greatest number
2. Write a C program to accept the length of three sides of a triangle from console and to test
and print the type of triangle –equilateral, isosceles, right angled, none of these.
Theory:
1. if statement
2. multiline if statement
Write explanation, syntax, semantic, example of each one is done for you
if statement
The if in C is the most simple decision-making statement. It consists of the test condition and
if block or body. If the given condition is true only then the if block will be executed.
Syntax of if Statement in C
if(condition)
{
// if body
- 13 -
Department of
Flow chart of if
int gfg = 9;
// if statement with true condition
if (gfg < 10)
printf("%d is less than 10", gfg);
return 0;
}
Output
9 is less than 10
- 14 -
Department of
Algorithm:
1 algorithm of program 1.
2 algorithm of program 2
Flow Chart
1
- 15 -