0% found this document useful (0 votes)
32 views12 pages

SPL Lab 2

The document outlines a series of programming exercises and concepts related to C programming, including arithmetic, relational, logical, and bitwise operators. It provides examples of input and output for various tasks, such as calculating payment for products, determining travel time for cars, and computing employee salaries. Additionally, it includes a solved program for calculating a student's percentage score based on marks in four subjects.

Uploaded by

Rakib Mia
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)
32 views12 pages

SPL Lab 2

The document outlines a series of programming exercises and concepts related to C programming, including arithmetic, relational, logical, and bitwise operators. It provides examples of input and output for various tasks, such as calculating payment for products, determining travel time for cars, and computing employee salaries. Additionally, it includes a solved program for calculating a student's percentage score based on marks in four subjects.

Uploaded by

Rakib Mia
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/ 12

Lab-01 Exercise

1. In this problem, the task is to read a code of a product 1, the number of units of
product 1, the price for one unit of product 1, the code of a product 2, the number
of units of product 2 and the price for one unit of product 2. After this, calculate and
show the amount to be paid.

Input
The input contains two lines of data. In each line there will be 3 values: two integers and
a floating value.

Output
The output file must be a message like the following example where Value to Pay. The
value must be presented with 2 digits after the point.
Input Samples Output Samples
12 1 5.30 Value to Pay: 15.50 tk
16 2 5.10
13 2 15.30 Value to Pay: 51.40 tk
161 4 5.20
1 1 15.10 Value to Pay: 30.20 tk
2 1 15.10

2. Two cars (X and Y) leave in the same direction. The car X leaves with a constant
speed of 60 km/h and the car Y leaves with a constant speed of 90 km / h. In one
hour (60 minutes) the car Y can get a distance of 30 kilometers from the X car, in
other words, it can get away one kilometer for each 2 minutes. Read the distance (in
km) and calculate how long it takes (in minutes) for the car Y to take this distance
in relation to the other car.

Input
The input file contains 1 integer value.

Output
Print the necessary time followed by the message "minutes" that means minutes in
Portuguese.
Sample input output:
3. Write a program that reads an employee's number, his/her worked hour’s number in
a month and the amount he received per hour. Print the employee's number and
salary that he/she will receive at end of the month, with two decimal places. Don’t
forget to print the line's end after the result, otherwise you will receive
“Presentation Error”. Don’t forget the space before and after the equal signal
and after the U$.

Input
The input file contains 2 integer numbers and 1 value of floating point, representing the
number, worked hours’ amount and the amount the employee receives per worked hour.

Output
Print the number and the employee's salary, according to the given example, with a blank
space before and after the equal signal.

4. Little John wants to calculate and show the amount of spent fuel liters on a trip,
using a car that does 12 Km/L. For this, he would like you to help him through a
simple program. To perform the calculation, you have to read spent time (in hours)
and the same average speed (km/h). In this way, you can get distance and then,
calculate how many liters would be needed. Show the value with three decimal
places after the point.
Input
The input file contains two integers. The first one is the spent time in the trip (in hours).
The second one is the average speed during the trip (in Km/h).
Output
Print how many liters would be needed to do this trip, with three digits after the decimal
point.
LAB #2
OPERATORS

C supports a rich set of operators, which are symbols used within an expression to
specify the manipulations to be performed while evaluating that expression. C has the
following operators:

C 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 or unary plus

- Subtraction or unary minus

* Multiplication

/ Division

% Remainder after division (modulo division)

Example 1: Working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

The operators +, - and * computes addition, subtraction, and multiplication respectively


as you might have expected.

In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.

It is because both the variables a and b are integers. Hence, the output is also an integer.
The compiler neglects the term after the decimal point and shows answer 2 instead of
2.25.

The modulo operator % computes the remainder. When a=9 is divided by b=4, the
remainder is 1. The % operator can only be used with integers.

Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,

// Either one of the operands is a floating-point number


a/b = 2.5
a/d = 2.5
c/b = 2.5

// Both operands are integers


c/d = 2

C Increment and Decrement Operators


C programming has two operators increment ++ and decrement -- to change the value
of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.


These two operators are unary operators, meaning they only operate on a single operand.
Example 2: Increment and Decrement Operators
// Working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}

Output

++a = 11
--b = 99
++c = 11.500000
--d = 99.500000

Here, the operators ++ and -- are used as prefixes. These two operators can also be used
as postfixes like a++ and a--. Visit this page to learn more about how increment and
decrement operators work when used as postfix.

C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =

Operator Example Same as


= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example 3: Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

return 0;
}

Output

c=5
c = 10
c=5
c = 25
c=5
c=0
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is
true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.

Operator Meaning of Operator Example


== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0

Example 4: Relational Operators


// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

return 0;
}
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision
making in C programming.

Operator Meaning Example


Logical AND. True only if all If c = 5 and d = 2 then, expression ((c==5)
&&
operands are true && (d>5)) equals to 0.
Logical OR. True only if either If c = 5 and d = 2 then, expression ((c==5) ||
||
one operand is true (d>5)) equals to 1.
Logical NOT. True only if the
! If c = 5 then, expression !(c==5) equals to 0.
operand is 0
Example 5: Logical Operators
// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}

Output

(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program

(a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).


(a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
(a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
(a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
!(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

C Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators


& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

Example 6: Bitwise Operators


#include <stdio.h>
int main()
{
int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
int x, y;
x = a ^ b; // 00001100
y = ~a; // 11111010

printf("a ^ b = %d\n", x);


printf("~a = %d\n", y);

return 0;
}
C Sizeof Operator
The memory size (in bytes) of a data type or a variable can be found with the sizeof
operator:

#include <stdio.h>

int main() {
int myInt;
float myFloat;
double myDouble;
char myChar;

printf("%lu\n", sizeof(myInt));
printf("%lu\n", sizeof(myFloat));
printf("%lu\n", sizeof(myDouble));
printf("%lu\n", sizeof(myChar));

return 0;
}

Solved Program
Program: Write a program to take input of rollno and marks obtained by a student in 4
subjects of 100 marks each and display the rollno with percentage score secured.
Algorithm:
1. Start
2. Define variables: rollno, sub1, sub2, sub3, sub4, sum, score
3. Take input from keyboard for all the input variables
4. Calculate the sum of marks of 4 subjects and also calculate the percentage
score as:
5. sum = sub1 + sub2 + sub3 + sub4; score = (sum/400) * 100
6. Display the roll number and percentage score.
7. Stop
Code: (Use comments wherever applicable)
#include<stdio.h>
void main()
{
int rollno;
float sub1, sub2, sub3, sub4, , sum, score;
printf ("Enter Roll Number: ");
scanf("%d", &rollno);
printf ("\n Enter Marks in 4 Subjects:\n");
scanf("%f%f%f%f", &sub1, &sub2, &sub3, &sub4);
sum=sub1+sub2+sub3+sub4;
score = (sum/400)*100;
printf("\n Roll Number: %d", rollno);
printf ("\nPercentage score secured: %2.2f%c", score,'%');
}

Output:
Enter Roll Number: 25
Enter Marks in 4 Subjects: 50
75
85
62
Roll Number: 25
Percentage score secured: 68.00%

SAMPLE PROGRAMS
(Students are to code the following programs in the lab and show the output to
instructor/course Teacher)
Instructions
Write comment to make your programs readable.
Use descriptive variables in your programs (Name of the variables should show
their purposes)

Programs List
1. Write a program to calculate simple and compound interest.
2. Write a program to swap values of two variables using a third variable.
3. Write a program to illustrate the use of unary prefix and postfix increment
and decrement operators.

You might also like