0% found this document useful (0 votes)
36 views

C Assignment Before 10th April

C ASSIGNMENT

Uploaded by

netafo3832
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)
36 views

C Assignment Before 10th April

C ASSIGNMENT

Uploaded by

netafo3832
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/ 13

NAME – SAPTAK BISWAS

ROLL-98 SEC - D

University of Engineering and Management, Kolkata

Assignment, Second Semester-2024

Course Name: Programming for Problem Solving

Course Code: ESCCS201

-----------------------------------------------------------------------------------------------------------------------------

Group - A

1. A) Answer – fun() is called 6 times in total, once for each value from 6 down to 1.

B) Answer - Each element of the array occupies 2 bytes (since an integer takes 2 bytes). The
memory location of the 8th element would be. Memory location of 8th element=Memory location of
the first byte of the array+(Index of the element−1)×Size of each element Memory location of 8th
element=Memory location of the first byte of the array+(Index of the element−1)×Size of each
element

Plugging in the values:

Memory location of 8th element=1000+(8−1)×2=1000+14=1014Memory location of 8th


element=1000+(8−1)×2=1000+14=1014

So, the memory location of the 8th element of the array would be 1014.

C) Answer - The output of the code is unpredictable due to undefined behavior. It might print 4 if the
memory layout happens to contain 4 at the location where the fourth element would be if the array
were properly initialized, but there's no guarantee of this.

D) Answer - int a; declares an integer variable a.

int arr[5] = {1, 2, 3, 4, 5}; initializes an integer array arr with 5 elements {1, 2, 3, 4, 5}.

arr[1] = ++arr[1]; increments the value at index 1 of arr by 1. So now arr[1] becomes 3 (2 + 1).

a = arr[1]++; assigns the value of arr[1] (which is 3) to a, and then increments the value of arr[1] by 1.

So now a becomes 3, and arr[1] becomes 4.

arr[1] = arr[a++]; assigns the value at index a (which is arr[3], since a is 3) to arr[1]. So now arr[1]
becomes 4.

printf("%d,%d", a, arr[1]); prints the value of a (which is 4) and the value of arr[1] (which is also 4).

E) Answer - char p[20]; declares an array p capable of holding 20 characters.

char s[] = "UEM"; initializes a character array s with the string "UEM".

int length = strlen(s); calculates the length of the string s, which is 3.


int i; declares an integer variable i.

The for loop iterates from i = 0 to length - 1, which is from 0 to 2 (inclusive), since length is 3.

Inside the loop, p[i] = s[length - i]; attempts to store the characters of s in reverse order into array p.
However, there's an indexing issue here. It should be s[length - i - 1] to access the correct
characters of s.

printf("%s", p); prints the string stored in p.

However, due to the indexing issue mentioned above, the code tries to access memory beyond the
bounds of array s, leading to undefined behavior. It may result in printing garbage values or crashing
the program.

To fix this issue, the line inside the loop should be corrected to: p[i] = s[length - i - 1];

So, if we fix this issue, the corrected output would be "MEU".

F) Answer - The while loop is skipped entirely because i is initially set to 0 and remains 0 throughout
the program execution. Therefore, the fun() function is never called. After the loop, the program
continues to execute and prints "Hello".

G) Answer - Due to the truncation behavior, i will never exceed 4, and the loop will keep running
indefinitely, printing "Inside Loop" repeatedly. The output would be an infinite repetition of "Inside
Loop" until the program is manually terminated.

H) Answer - So, the output of the code will be:

7531

The fun() function starts with n = 1357.

It prints 7 (the last digit of 1357) and then calls itself with n = 135.

It prints 5 (the last digit of 135) and then calls itself with n = 13.

It prints 3 (the last digit of 13) and then calls itself with n = 1.

It prints 1 (the last digit of 1) and then calls itself with n = 0.

Since n is now 0, the recursion stops and the function returns.

Group – B

2. Answer - The output of the code will be:

12

12

We modified the swap() function to accept pointers to integers (int *a and int *b). Inside the swap()
function, we dereference these pointers to access and modify the values of the original variables.
We call swap(&a, &b) in main() to pass the addresses of a and b to the swap() function, so it can
modify their values directly.
3. Answer - #include<stdio.h>

if (n <= 1)

return n;

else

return fibonacci(n-1) + fibonacci(n-2);

int main()

int n, i;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

for (i = 0; i < n; i++)

printf("%d ", fibonacci(i));

return 0;

4. Answer - *(q + 1) accesses the value at the memory location q + 1, which is the second element
of the integer array x. So, *(q + 1) evaluates to 12. *(q + 2) accesses the value at the memory
location q + 2, which is the third element of the integer array x. So, *(q + 2) evaluates to 10.
Therefore, *(q + 1) - *(q + 2) evaluates to 12 - 10 = 2. Now, p + *(q + 1) - *(q + 2) is equivalent to p + 2,
which moves the pointer p two positions ahead. So, *(p + *(q + 1) - *(q + 2)) accesses the character
at the memory location p + 2, which corresponds to the third character of the string "termexam",
i.e., 'r'. Therefore, the output of the code will be:

It prints the character 'r'.

5. Answer - #include <stdio.h>

int findMinimum(int arr[], int size) {

int min = arr[0]; // Assume the first element as minimum


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

if (arr[i] < min) {

min = arr[i];

return min;

int main() {

int arr[5]; // Declare an array to store five integers

int i;

printf("Enter five integers:\n");

for (i = 0; i < 5; i++) {

scanf("%d", &arr[i]);

int minimum = findMinimum(arr, 5);

printf("The minimum of the five numbers is: %d\n", minimum);

return 0;

6. Answer - #include <stdio.h>

#include <string.h>

int main() {

char password[50];

printf("Enter your password: ");

scanf("%s", password);

if (strlen(password) >= 4) {

printf("Password Accepted\n");

} else {

printf("Incorrect Password\n");

}
return 0;

7. Answer - #include <stdio.h>

int main() {

int x[6] = {1, 2, 3, 5, 7, 9};

int val, index;

printf("Enter the value to check: ");

scanf("%d", &val);

printf("Enter the index: ");

scanf("%d", &index);

if (index >= 0 && index < 6) {

if (x[index] == val) {

printf("%d is present at position %d in the array.\n", val, index);

} else {

printf("%d is not present at position %d in the array.\n", val, index);

} else {

printf("Invalid index. Please enter an index between 0 and 5.\n");

return 0;

Group – C

8. Answer - #include <stdio.h>

unsigned long long factorial(int n) {

if (n == 0 || n == 1) {

return 1; // 0! and 1! are both 1

} else {

unsigned long long result = 1;

for (int i = 2; i <= n; i++) {


result *= i;

return result;

unsigned long long seriesSum(int n) {

unsigned long long sum = 0;

for (int r = 0; r <= n; r++) {

sum += factorial(n) / (factorial(r) * factorial(n - r));

return sum;

int main() {

int n;

printf("Enter the value of n: ");

scanf("%d", &n);

unsigned long long sum = seriesSum(n);

printf("The sum of the series is: %llu\n", sum);

return 0;

9. Answer - #include <stdio.h>

void determineBestArcher(int A[], int K[]) {

int arjunaWins = 0, karnaWins = 0;

for (int i = 0; i < 5; i++) {

if (A[i] > K[i]) {

arjunaWins++;

} else if (K[i] > A[i]) {

karnaWins++;

}
}

if (arjunaWins > karnaWins) {

printf("Arjuna\n");

} else if (karnaWins > arjunaWins) {

printf("Karna\n");

} else {

printf("Tie\n");

int main() {

int A[5], K[5];

printf("Enter the scores for Arjuna:\n");

for (int i = 0; i < 5; i++) {

printf("Level %d: ", i + 1);

scanf("%d", &A[i]);

printf("Enter the scores for Karna:\n");

for (int i = 0; i < 5; i++) {

printf("Level %d: ", i + 1);

scanf("%d", &K[i]);

determineBestArcher(A, K);

return 0;

10. A) Answer - #include <stdio.h>

#include <string.h>

int main() {

char t, s[10];

int i, j, n;
printf("Enter a string: ");

scanf("%s", s);

n = strlen(s);

for (i = 0, j = n - 1; i < j; i++, j--) {

t = s[i];

s[i] = s[j];

s[j] = t;

printf("Reverse of the string: %s\n", s);

return 0;

B) Answer - #include <stdio.h>

int findLength(char s[]) {

int length = 0;

while (s[length] != '\0') {

length++;

return length;

int main() {

char t, s[10];

int i, j, n;

printf("Enter a string: ");

scanf("%s", s);

n = findLength(s); // Using the custom function to find the length of the string

for (i = 0, j = n - 1; i < j; i++, j--) {

t = s[i];

s[i] = s[j];

s[j] = t;

}
printf("Reverse of the string: %s\n", s);

return 0;

11. A) Answer - int perfect(int n) {

int sum = 0;

for (int i = 1; i <= n / 2; i++) {

if (n % i == 0) {

sum += i;

if (sum == n) {

return 1; // 'n' is a perfect number

} else {

return 0; // 'n' is not a perfect number

B) Answer - int GCD(int x, int y) {

int remainder;

while (y != 0) {

remainder = x % y;

x = y;

y = remainder;

return x;

12. A) Answer - #include <stdio.h>

int main() {

int A[50], B[50], merged[100];

int sizeA, sizeB;


printf("Enter the size of array A: ");

scanf("%d", &sizeA);

printf("Enter elements of array A: ");

for (int i = 0; i < sizeA; i++) {

scanf("%d", &A[i]);

printf("Enter the size of array B: ");

scanf("%d", &sizeB);

printf("Enter elements of array B: ");

for (int i = 0; i < sizeB; i++) {

scanf("%d", &B[i]);

int mergedSize = sizeA + sizeB;

int index = 0;

for (int i = 0; i < sizeA; i++) {

merged[index++] = A[i];

for (int i = 0; i < sizeB; i++) {

merged[index++] = B[i];

printf("Merged array: {");

for (int i = 0; i < mergedSize; i++) {

printf("%d", merged[i]);

if (i < mergedSize - 1) {

printf(", ");

printf("}\n");

return 0;
}

B) Answer - #include <stdio.h>

int isBalanced(int matrix[3][3]) {

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (matrix[i][j] != matrix[j][i]) {

return 0; // Matrix is not balanced

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (i == j && matrix[i][j] != matrix[0][0]) {

return 0; // Matrix is not balanced

return 1; // Matrix is balanced

int main() {

int matrix[3][3];

printf("Enter elements of the 3x3 matrix:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

scanf("%d", &matrix[i][j]);

if (isBalanced(matrix)) {

printf("The matrix is balanced.\n");


} else {

printf("The matrix is not balanced.\n");

return 0;

13. A) Answer - #include <stdio.h>

int power(int x, int n) {

// Base case: If power is 0, return 1

if (n == 0) {

return 1;

else {

return x * power(x, n - 1);

int main() {

int x, n;

printf("Enter the base (x): ");

scanf("%d", &x);

printf("Enter the exponent (n): ");

scanf("%d", &n);

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

return 0;

B) Answer - #include <stdio.h>

int duplicate(int *A, int k) {

int index = -1; // Initialize index to -1, indicating k is not found initially

for (int i = 0; A[i] != '\0'; i++) {

if (A[i] == k) {
index = i; // Update index to the current index if k is found

break; // Break out of the loop after finding the first occurrence

return index; // Return the index of the first occurrence of k

int main() {

int A[] = {1, 2, 5, 2, 1};

int k = 2;

printf("Index of the first occurrence of %d: %d\n", k, duplicate(A, k));

return 0;

-----------------------------------------------------------------------------------------------------------------------------

You might also like