0% found this document useful (0 votes)
6 views15 pages

Writeup Points For Lab Assignment 2,3

Uploaded by

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

Writeup Points For Lab Assignment 2,3

Uploaded by

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

Department of

Assignment No.2

Title: Write a Program in c to demonstrate Arithmetic Operators (+,-,*,/,%).

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.

Introduction to the Operators


An operator in C can be defined as the symbol that helps us to perform some specific
mathematical, relational, bitwise, conditional, or logical computations on values and variables.
The values and variables used with operators are called operands. So we can say that the
operators are the symbols that perform operations on operands.
Types of operators
C language provides a wide range of operators that can be classified into 6 types based on their
functionality:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators

Arithmetic operators
The operators which are used to perform arithmetic/mathematical operations on operands there
are 9 arithmetic operators in c.

-1-
Department of

S. No. Symbol Operator Description Syntax

Adds two numeric


+ Plus a+b
1 values.

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.

Flips the sign of


– Unary Minus -a
7 the value.

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

S. No. Symbol Operator Description Syntax

Returns true if the


left operand is less
< Less than than the right a<b
operand. Else
1 false

Returns true if the


left operand is
> Greater than greater than the a>b
right operand. Else
2 false

Returns true if the


left operand is less
Less than or
<= than or equal to a <= b
equal to
the right operand.
3 Else false

Returns true if the


left operand is
Greater than or greater than or
>= a >= b
equal to equal to right
operand. Else
4 false

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.

S. No. Symbol Operator Description Syntax

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.

Returns true if the


! Logical NOT !a
3 operand is false.

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

S. No. Symbol Operator Description Syntax

Performs bit-by-
bit XOR operation
^ Bitwise XOR a^b
and returns the
3 result.

Flips all the set


Bitwise First
~ and unset bits on ~a
Complement
4 the number.

Shifts the number


in binary form by
<< Bitwise Leftshift one place in the a << b
operation and
5 returns the result.

Shifts the number


in binary form by
Bitwise
>> one place in the a >> b
Rightshilft
operation and
6 returns the 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 :

S. No. Symbol Operator Description Syntax

Assign the value


Simple of the right
= a=b
Assignment operand to the left
1 operand.

2 += Plus and assign Add the right a += b


operand and left
operand and assign
this value to the

-5-
Department of

S. No. Symbol Operator Description Syntax

left operand.

Subtract the right


operand and left
-= Minus and assign operand and assign a -= b
this value to the
3 left operand.

Multiply the right


operand and left
Multiply and
*= operand and assign a *= b
assign
this value to the
4 left operand.

Divide the left


operand with the
/= Divide and assign right operand and a /= b
assign this value to
5 the 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

9 ^= XOR and assign Performs bitwise a ^= b


XOR and assigns

-6-
Department of

S. No. Symbol Operator Description Syntax

this value to the


left operand.

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.

We may replace the use of if..else statements with conditional operators.

Syntax

operand1 ? operand2 : operand3;

dot (.) and arrow (->) Operators


Member operators are used to reference individual members of classes, structures, and
unions.
The dot operator is applied to the actual object.
The arrow operator is used with a pointer to an object.
Syntax
structure_variable . member;

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);

After substitution of values into the given expression, we get:

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

a+b*c > a*b+c

a=3 b=2 c=1

z=a*b>a+c

Flow of execution

z=3*2>3+1

z=6>4

condition 6>4 is true so 1 is assigned to z

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:

c= (a+b)>c && a<b

a>b || b>a

-9-
Department of

From the given below logical expression example, let's consider the values of operands to
be

a=2, b=4, and c=3.

And the expression is

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:

Exp1? Exp2: Exp3

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

a*b>c ? true : false

From the given below conditional expressions example, let's consider the values of operands to be
a=5, b=3, and c=1.

After substitution of values into the given expression, we get:

Z = (5 * 3) < 1 ? ‘true’ : ‘false’

- 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

EXPECTED OUTPUT OF PROGRAM 1:-

Enter First Number: 30

Enter Second Number: 10

Addition is:- 40

Subtraction is:-20

Multiplication is:-300

Division is:-3

Modulus is :-0

- 11 -
Department of

EXPECTED OUTPUT OF PROGRAM 2:-

Enter Principal Amount:- 200000

Enter Rate of Interest:- 11.3

Enter Time Duration :- 17.6

Simple Interest is :- 397760.000000

Simple Interest in exponetional form is :- 3.977600e+05

EXPECTED OUTPUT OF PROGRAM 3:-

Enter length of Radius of circle:-

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.

Pre-requisite: Turbo C, basic header files, IO statements in C, Operators. if------ else-if


Statement in C.

Theory:

The Decision Making Statements in C

1. if statement

2. multiline if statement

3. if----- else statement

4. if---- else 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.

The if in C is a decision-making statement that is used to execute a block of code based on


the value of the given expression. It is one of the core concepts of C programming and is
used to include conditional code in our program.

Syntax of if Statement in C

if(condition)
{
// if body

- 13 -
Department of

// Statements to execute if condition is true


}

Flow chart of if

How to use if statement in C?


The following examples demonstrate how to use the if statement in C:
int main()
{

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

EXPECTED OUTPUT OF PROGRAM 1:-

Enter First Number: 30

Enter Second Number: 10

Enter Third Number:-45

Greatest Number is:-45

EXPECTED OUTPUT OF PROGRAM 3:-

Enter the length of three sides of a triangle:


12
12
10
Triangle is Isosceles Triangle

- 15 -

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy