0% found this document useful (0 votes)
45 views9 pages

Lab Report CSE162.5 Blank

baiugvdklba;iwusyh

Uploaded by

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

Lab Report CSE162.5 Blank

baiugvdklba;iwusyh

Uploaded by

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

Southeast University, Bangladesh

School of Science and Engineering


Department of Computer Science and Engineering

Course Code : CSE162

Course Title : Programming Language I Lab

Lab Report

Submitted By
Name :

Batch :

Student Code :

Section :

Semester : Summer 2024

Submitted To
Tashreef Muhammad
Lecturer, Dept. of CSE,
Southeast University, Bangladesh
Table of Contents
Problem 01:--------------------------------------------------------------------------------------------------
Problem 02:--------------------------------------------------------------------------------------------------
Problem 03:--------------------------------------------------------------------------------------------------
Problem 04:--------------------------------------------------------------------------------------------------
Problem 01:
Problem Link:
Divisibility Problem

Solution Approach:

Input Parsing: We start by reading the number of test cases t.


Loop Through Test Cases: For each test case,
we Read integers a and b.
Calculate the remainder of a % b to check if a is already divisible by b.
Divisibility Check:
If the remainder is 0, a is already divisible by b, so no moves are needed (0 is printed).
If the remainder is not 0, the required moves to make a divisible by b is calculated as b
- remainder.
Output: The result for each test case is printed.

Solution Code:
#include <stdio.h>

int main()
{
int t;
scanf("%d", &t); // Read the number of test cases

while (t--) {
long long a, b;
scanf("%lld %lld", &a, &b); // Read integers a and b

long long remainder = a % b; // Calculate the remainder of a divided by b

if (remainder == 0) {
printf("0\n"); // If remainder is 0, no moves are needed
} else {
long long moves = b - remainder; // Calculate the moves needed to make a
divisible by b
printf("%lld\n", moves);
}
}

return 0;
}
Problem 02:
Problem Link:
Candies and Two Sisters

Solution Approach:
Input Parsing: We start by reading the number of test cases t.
Loop Through Test Cases: For each test case:
We read the integer n, the total number of candies.
Calculate the number of ways to distribute candies such that Alice gets more
than Betty:
Since Alice needs to get more than Betty, the valid distributions occur when
Alice gets from (n/2 + 1) to (n - 1) candies. This can be simplified to (n - 1) / 2,
which represents the number of possible ways.
Output: Print the calculated number of ways for each test case.
This solution is efficient and meets the conditions of the problem.

Solution Code:

#include <stdio.h>

int main() {
int t;
scanf("%d", &t); // Read the number of test cases

while (t--) {
long long n;
scanf("%lld", &n); // Read the number of candies for each test case

// Calculate the number of ways to distribute the candies


long long ways = (n - 1) / 2;

printf("%lld\n", ways);
}

return 0;
}
Problem 03:
Problem Link:
Medium Number

Solution Approach:
Input Parsing: We first read the number of test cases t.
Loop Through Each Test Case:
For each test case, we read three distinct integers a, b, and c.
We then check which integer is the middle number:
If a is between b and c, then a is the middle.
If b is between a and c, then b is the middle.
Otherwise, c is the middle.
Output: We print the middle number for each test case.
This approach ensures we find the medium number in a straightforward way.

Solution Code:
#include <stdio.h>

int main() {
int t;
scanf("%d", &t); // Read the number of test cases

while (t--) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c); // Read three distinct integers

// Find the middle number


int middle;
if ((a > b && a < c) || (a > c && a < b)) {
middle = a;
} else if ((b > a && b < c) || (b > c && b < a)) {
middle = b;
} else {
middle = c;
}

printf("%d\n", middle); // Output the middle number


}

return 0;
}
Problem 04:
Problem Link:
Inverse Prefix Sum
Solution Approach:
Read Input:
The program starts by reading an integer N, which is the length of the sequence S.
Declare Arrays:
Two arrays are declared:
S (to store the input sequence) and
A (to store the new sequence we're calculating).
Fill Sequence S:
The program reads N numbers from the input and stores them in the S array.
Calculate Sequence A:
The first element of 𝐴 is set to be the same as the first element of S, and this value is
printed.
For the remaining elements in A, each A[i] is calculated as the difference between S[i]
and S[i−1] (the previous element of S).
This difference is then printed immediately after it is calculated, with a space in
between each printed element.
Output:
The entire sequence A is printed on a single line.
Solution Code:
#include <stdio.h>
int main() {
int N;
scanf("%d", &N); // Read the length of the sequence S

long long S[N], A[N]; // Declare arrays to store S and A


for (int i = 0; i < N; i++) {
scanf("%lld", &S[i]); // Read elements of sequence S
}
// Calculate the elements of sequence A
A[0] = S[0]; // The first element of A is the same as the first element of S
printf("%lld", A[0]); // Print the first element of A

for (int i = 1; i < N; i++) {


A[i] = S[i] - S[i - 1]; // Calculate each subsequent element of A
printf(" %lld", A[i]); // Print each element with a space
}
printf("\n");

return 0;
}

You might also like