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

Lab 2

Notes for laab

Uploaded by

kdbinoye
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)
11 views8 pages

Lab 2

Notes for laab

Uploaded by

kdbinoye
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

Programming in C (CS1L002)

Lab Sheet 2

Topics to be covered in the lab:


1.​ArithmeticOperators
2.​Logical Operators
3.​Relational Operators
4.​Conditional control structure

INSTRUCTIONS: The notes and examples are given for your understanding. Solve all the
exercise questions and submit in the lab assignment shared in the google classroom.
Your Solution Codes must be properly indented and commented.

Arithmetic operators:
The symbols of the arithmetic operators are:-

●​ Pre-increment Operator: The pre-increment operator is used to increase the original value of
the operand by 1 before assigning it to the expression.

Y=++X; In this syntax, the value of operand 'X' is increased by 1, and then a new value is
assigned to the variable 'Y'.
●​ Post increment Operator: The post-increment operator is used to increment the original value of
the operand by 1 after assigning it to the expression.

Y=X++; In this syntax, the value of operand 'X' is assigned to the variable 'Y'. After that, the
value of variable 'X' is incremented by 1.

●​ Pre-Decrement Operator: The pre-increment operator is used to increase the original value of
the operand by 1 before assigning it to the expression.

Y=--X; In this syntax, the value of operand 'X' is decreased by 1, and then a new value is assigned
to the variable 'Y'.

●​ Post Decrement Operator: The post-increment operator is used to increment the original value
of the operand by 1 after assigning it to the expression.

Y=X--; In this syntax, the value of operand 'X' is assigned to the variable 'Y'. After that, the value
of variable 'X' is decreased by 1.

Example:

Use of increment and decrement operators:

#include<stdio.h>
int main ()
{ /* declare integer variables */
int x, y, z, a, b, c,d;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" Input the value of Y: ");
scanf (" %d", &y);
printf (" Input the value of Z: ");
scanf (" %d", &z);
/* use post-increment operator to update the value by 1 */
a = x++;
b = ++y;
c = z--;
d=--z;
printf (" \n The original value of a: %d", a);
printf (" \n The original value of b: %d", b);
printf (" \n The original value of c: %d", c);
printf (" \n The original value of c: %d", c);
printf (" \n\n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}

Logical Operators

Operator Description

&& Logical AND operator. If both the operands are non-zero, then the condition becomes true.

|| Logical OR operator. If any of the two operands is non-zero, then the condition becomes true.

! Logical NOT operator. It is used to reverse the logical state of its operand. If a condition is
true, then Logical NOT operator will make it false.

Relational Operators
Operator Description

== Checks if the values of two operands are equal or not. If yes, then the condition
becomes true.

!= Checks if the values of two operands are equal or not. If values are not equal then
the condition becomes true.

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

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

>= Checks if the value of left operand is greater than or 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 equal to the value of right
operand. If yes, then the condition becomes true.

We shall be using these logical and relational operators in decision control statements
and looping constructs.

Conditional Control Structure


Normally, program flows along line by line in the order in which it appears in the source
code. But, it is sometimes required to execute a particular portion of code only if certain
condition is true or false i.e. we have to make decision in our program. There are three
major decision making structures.

The one way decision using if statement


The if statement enables us to test for a condition (such as whether two variables are equal)
and branch to different parts of the code, depending on the result of the conditions
constructed using relational and logical.

The simplest form is

if (TestExpr)
{

block of statements executed when “if” condition is true;

Two way decision using if-else statement


If-else statement is used when we want to take one branch if the condition is true, another
if it is false. The form of if-else is as follows

if (TestExpr)

block of statements executed when “if” condition is true;

else

block of statements executed when “if” condition is false

Multi-way Decision
Multi-way decision statements use if-else-if and nested ifs. They are used to evaluate a test
expression that could have several possible values. It is often used to choose between ranges of
values. The form of multi-way decision is as follows

if (TestExpr)
{

block of statements executed when “if” condition is true;

else if(TestExpr)

block of statements executed when “else if” condition is true;

else

block of statements executed when none of the above conditions are true

Example: Comparing two variables

#include <stdio.h>
int main()
{
int x, y;
printf("enter the value of x:");
scanf("%d", &x);
printf("enter the value of y:");
scanf("%d", &y);
if (x>y)
{
​ printf("x is greater than y\n");
}
if (x<y)
{
​ printf("x is less than y\n");
}
if (x==y)
{
​ printf("x is equal to y\n");
}
printf("End of Program");
return 0;
}
Few useful functions:
C provides library functions for performing mathematical operations like squareroot, exponentiation
etc. These functions are defined in the header file math.h.

●​ double cos(double x) - Returns the cosine of a radian angle x.


●​ double sin(double x) - Returns the sine of a radian angle x.
●​ double exp(double x) - Returns the value of e raised to the xth power.
●​ double log(double x) - Returns the natural logarithm (base-e logarithm) of x.
●​ double log10(double x) - Returns the common logarithm (base-10 logarithm) of x.
●​ double pow(double x, double y) - Returns x raised to the power of y.
●​ double sqrt(double x) - Returns the square root of x.
●​ double ceil(double x) - Returns the smallest integer value greater than or equal to x.
●​ double fabs(double x) - Returns the absolute value of x.
●​ double floor(double x) - Returns the largest integer value less than or equal to x.

Exercises:
1.​ Write a C program which takes three sides (a,b,c) of a triangle as input and
calculates its area, if the triangle inequality conditions are satisfied :
(i) a+b>c, (ii) b+c>a, (iii) a+c>b.
Area = √(s(s-a)(s-b)(s-c))
where:
S = (a+b+c)/2
S is the semi-perimeter
Otherwise print the error.

2.​ Write a program which takes 2 numbers as input from the user and determines
whether one is a factor of the other.

3.​ Write a C program that takes a person’s age and classifies them as: Child (0–12),
Teen (13–19), Adult (20–59), Senior (60 and above).

4.​ A shop offers discounts as follows:​


- Bill < ₹1000: No discount​
- ₹1000–₹5000: 10% discount​
- >₹5000: 20% discount​
Write a C program to input the bill amount and print the final payable amount.

5.​ Write a program that returns a letter grade based on a quiz score. The input
given by the user will be the integer score from a ten point quiz. The letter
grades are assigned assigned as follows:
Score Grae
9 – 10 “A”
7–8 “B”
5–6 “C”
3–4 “D”
<3 “F”
Print the GPA and whether the student passed (GPA > 3) or failed.

You might also like