# Introduction to Programming in C Week 5

**Session: JAN-APR 2024**

**Course Name: Introduction to Programming in C**

**Course Link: Click Here**

**For answers or latest updates join our telegram channel: Click here to join **

**These are Introduction to Programming in C Assignment 5 Answers**

**Question 1**

**Convergence depth of Collatz functionThe Collatz function is defined for a positive integer n as follows.f(n)={3n+1,n/2,if n oddif n is evenSample Input7Sample Output16**

**Solution:**

```
#include <stdio.h>
int collatzConvergenceDepth(int n) {
int count = 0;
while (n != 1) {
if (n % 2 == 0) {
n /= 2;
} else {
n = 3 * n + 1;
}
count++;
}
return count;
}
int main() {
int n;
scanf("%d", &n);
int depth = collatzConvergenceDepth(n);
printf("%d", depth);
return 0;
}
```

**For answers or latest updates join our telegram channel: Click here to join **

**These are Introduction to Programming in C Assignment 5 Answers**

**Question 2**

**BlockSum of an ArrayGiven an integer array M having size n which is power of 2, Write a recursive code to find the BlockSum of the array M.The following is the recursive definition of BlockSum:If size of M is 2, say M=[a,b], where a and b are integers, then BlockSum(M)=a−b.**

**Solution:**

```
#include <stdio.h>
int blockSum(int arr[], int n) {
if (n == 2) {
return arr[0] - arr[1];
} else {
int mid = n / 2;
int sumA = blockSum(arr, mid);
int sumB = blockSum(arr + mid, mid);
return sumA - sumB;
}
}
int main() {
int n;
scanf("%d", &n);
int arr[1024];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int result = blockSum(arr, n);
printf("%d", result);
return 0;
}
```

**For answers or latest updates join our telegram channel: Click here to join **

**These are Introduction to Programming in C Assignment 5 Answers**

More Solutions of Introduction to Programming in C: Click Here

More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/

**Course Name: Introduction to programming in C**

**Course Link: Click Here**

**These are Introduction to Programming in C Assignment 5 Answers**

**Question 1**

**Write a recursive program that inputs a line of characters from the user.**

The line may contain blanks. It outputs the line with the characters reversed. The input ends with EOF (end of file).

NOTE: You have to use recursion to solve this, and are NOT allowed to use array to store the input!!

Example:

INPUT

This is easy

OUTPUT

ysae si sihT

The line may contain blanks. It outputs the line with the characters reversed. The input ends with EOF (end of file).

NOTE: You have to use recursion to solve this, and are NOT allowed to use array to store the input!!

Example:

INPUT

This is easy

OUTPUT

ysae si sihT

**These are Introduction to Programming in C Assignment 5 Answers**

**Code:-**

```
#include <stdio.h>
void revstr(){
int ch = getchar();
if (ch == EOF) return;
revstr();
putchar(ch);
}
int main(){
revstr();
return 0;
}
```

**These are Introduction to Programming in C Assignment 5 Answers**

**Question 2**

**The Collatz function is defined for a positive integer n as follows.f(n)={3n+1,n/2,if n oddif n is evenWe consider the repeated application of the Collatz function starting with a given integer n, asfollows:f(n),f(f(n)),f(f(f(n))),…It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260 [Wikipedia: Collatz Conjecture].**

**These are Introduction to Programming in C Assignment 5 Answers**

**e.g. If n=7, the sequence isf(7) = 22f(f(7)) = f(22) = 11f(11) = 34f(34) = 17f(17) = 52f(52) = 26f(26) = 13f(13) = 40f(40) = 20f(20) = 10f(10) = 5f(5) = 16f(16) = 8f(8) = 4f(4) = 2f(2) = 1Thus if you start from n=7, you need to apply f 16 times in order to first get 1.In this question, you will be given a positive number <= 32,000. You have to output how manytimes f has to be applied repeatedly in order to first reach 1.**

**These are Introduction to Programming in C Assignment 5 Answers**

**Code:-**

```
#include <stdio.h>
int collatz(int m);
int c=0;
int main()
{
int n,c;
scanf("%d",&n);
if(n<=0)
{
printf("0");
}
else if(n==1)
{
printf("0");
}
else
{
c = collatz(n);
printf("%d",c);
}
}
int collatz(int m)
{
int n=m;
if(n%2==0)
{
n=n/2;
c++;
if(n!=1)
{
collatz(n);
}
else
{
return c;
}
}
else
{
n=(3*n)+1;
c++;
if(n!=1)
{
collatz(n);
}
else
{
return c;
}
}
}
```

**These are Introduction to Programming in C Assignment 5 Answers**

**Question 3**

**DescriptionWrite a recursive code to find the block product of an array.The following is the algorithm to find the block product recursively:Given an array A partition it into four quadrants of equal size:M=[ABCD]The block product of the array is defined recursively as :|M|=|A|∗|D|−|B|∗|C|With the base case, when n = 4 , |[a,b,c,d]|=ad−bcNote : You can assume that n is a power of 4. n>=4 and n <= 1024.**

**These are Introduction to Programming in C Assignment 5 Answers**

**InputThe first line contains the array size nThe next n lines contains the elements of the array.OutputBlock product |M|Example 1M=[3215]|M|=3∗5−2∗1=13Example 2M=[32152−25510750−256]**

**Code:-**

`//Code`

**These are Introduction to Programming in C Assignment 5 Answers**

More Weeks of Introduction to programming in C: Click Here

More Nptel courses: https://progiez.com/nptel

**These are Introduction to Programming in C Assignment 5 Answers**