0% found this document useful (0 votes)
3 views8 pages

Lab Manual 02

The document is a lab manual for CSC1102 Programming Language 1 at American International University-Bangladesh, focusing on the C programming language operators including arithmetic, conditional, relational, equality, and logical operators. It outlines prerequisites, objectives, theoretical explanations, and provides example programs for each type of operator. Additionally, it includes exercises and assignments for practical application of the concepts learned.

Uploaded by

irtehanpiyal
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)
3 views8 pages

Lab Manual 02

The document is a lab manual for CSC1102 Programming Language 1 at American International University-Bangladesh, focusing on the C programming language operators including arithmetic, conditional, relational, equality, and logical operators. It outlines prerequisites, objectives, theoretical explanations, and provides example programs for each type of operator. Additionally, it includes exercises and assignments for practical application of the concepts learned.

Uploaded by

irtehanpiyal
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/ 8

AMERICAN INTERNATIONAL UNIVERSITY-BANGLADESH (AIUB)

FACULTY OF SCIENCE & TECHNOLOGY


DEPARTMENT OF COMPUTER SCIENCE

LAB MANUAL 02
CSC1102 Programming Language 1 [EEE]
TITLE

A Brief Introduction to the C Programming Language Operators i.e. Arithmetic, Conditional,


Relational, Equality and Logical Operators

PREREQUISITE

• To be able to write, build and run a C program in CodeBlocks


• To be able to identify and understand the basic components of a C program
• To be able to use the library functions printf() and scanf()
• To have basic idea about variable and type casting

OBJECTIVE

• To know about Arithmetic Operators


• To know about Conditional Operator
• To know about Relational Operators
• To know about Equality Operators
• To know about Logical Operators
• To be able to solve the exercises, lab work and assignments at the end of this manual

THEORY

Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C language is rich in built-in operators.

Arithmetic Operators

Following table shows all the arithmetic operators supported by C language. Assume
variable A holds 10 and variable B holds 20.
Below is a program that creates and assigns values to two integer variables and adds them
using arithmetic operator (+)

#include <stdio.h>
int main()
{ int A=10;
int B=20;
int C;
C=A+B;
printf("value becomes %d\n", C);
return 0;
}

Conditional Operator

The conditional operator is also known as Ternary Operas or ?: operator. It takes on three
arguments.

The general form for conditional operator is expression 1? expression 2 : expression 3

where expression1 is Condition, expression2 is Statement Followed if Condition is True

and expression3 is Statement Followed if Condition is False

Below is a program that takes a value of A. Checks if it is equal to 4 (Will explain ==

operator later on). If true prints 1 and if false prints 2

int main()
{
int A=3;
int C;
C=(A==4 ? 1 : 2);
printf("value becomes %d\n", C);
}

Relational Operators

Following table shows all the relational operators supported by C language. Assume variable
A holds 10 and variable B holds 20

Operator Description Example

Below is a program that creates and assigns value to a variable A. Checks if it is greater than
3. If true 1 is printed and if false 2 is printed.
#include <stdio.h>
int main()
{
int A=10;
int C;
C=(A>3 ? 1 : 2);
printf("value becomes %d\n", C);
}

Equality Operators

Following table shows all the equality operators supported by C language. Assume variable A
holds 10 and variable B holds 20
Below is a program that takes a variable A=10 checks if it is not equal to 3 and gives output.

#include <stdio.h>
int main()
{
int A=10;
int C;
C=(A!=3 ? 1 : 2);
printf("value becomes %d\n", C);
}

Logical Operators

Following table shows all the logical operators supported by C language. Assume variable A

holds 1 and variable B holds 0

Below is a program that takes two variable and check if there values match.

#include <stdio.h>
int main()
{
int A=1;
int B=0;
int C;
C=(A&&B ? 1 : 2);
printf("value becomes %d\n", C);
}
Operator Precedence in C

Operator precedence determines the grouping of terms in an expression. This affects how
an expression is evaluated. Certain operators have higher precedence than others; for
example, the multiplication operator has higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.

Run the program below and check the precedence of the operators

#include <stdio.h>
int main()
{
int a = 6;
int b = -3;
int c = 2;
int d;
d= a - b * (a + c * 2) + a / 2 * b;
printf("Value of c = %d \n", d);
}

EXERCISE
I. Increment operator _____________ integer value by one.
II. The conditional operator takes arguments.
III. checks if the value of left operand is greater than value of right operand.
IV. && is called logical operator.
V. Certain operators have precedence than others.

LAB WORK
I. Write a program that takes two inputs from the user and adds, subtracts, multiplies
and divide it and also print the output.
II. Write a program that creates two integer variables and assigns value to them then
divide the first variable from the second variable and give output in float number.
III. Write a program that take two integer variable as input from the user. If variable a is
greater or equal to variable b output is 1 else output is 0.
IV. Write a program that takes one integer variable as input from the user and checks if
it is even.
V. Write a program that calculates the equivalent resistance of the following circuit

Where R1=5 ohm, R2=3 ohm and R3=9 ohm.


VI. Write a program that calculates the CGPA of a student who is in 1 st Semester

Course Grade
Electric Circuit 1 3.75
Math 1 4.0
Physics 1 3.75
English 1 3.5

VII. Write a program that will ask for a resistance in kilo ohm and display the output in
ohm.

ASSIGNMENT

I. Write a program that takes two voltage input voltage1=5V and voltage2=12V and
print their average.
II. Write a program that takes length and breadth as input from the user and prints the
area of a rectangle as output.
Hint: area=length*breadth

III. Write a program that calculates the area of an equilateral triangle where the length
of one side a=5.
IV. Write a program that converts 1 degree Celsius into its equivalent Kelvin value.
V. Write a program that calculates the equivalent resistance of the following circuit

Where R1=12 ohm, R2=3 ohm, R3=9 ohm and R4=16 ohm.
VI. Write a program that calculates the equivalent resistance of the following circuit

Where R1=5 ohm, R2=12 ohm and R3=8 ohm.

You might also like