# Introduction to programming in C Week 5

**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

**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.**

**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.**

**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]|M|=∣∣∣[3215]∣∣∣∗∣∣∣[0−256]∣∣∣−∣∣∣[2−255]∣∣∣∗∣∣∣[1075]∣∣∣=13∗10−20∗5=30**

**Code:-**

```
#include <stdio.h>
int product(int *arr, int n)
{
if(n==4)
{
return(arr[0]*arr[3]-arr[1]*arr[2]);
}
int *a=arr,*b=arr+(n/4),*c=arr+(n/2),*d=arr+(3*n/4);
int M1=product(a,n/4);
int M2=product(b,n/4);
int M3=product(c,n/4);
int M4=product(c,n/4);
return (M1*product(d,n/4)-M2*product(c,n/4));
}
int main()
{
int n,i,res;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
res=product(arr,n);
printf("%d",res);
return 0;
}
```

**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

* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first. COPYING MATERIALS IS STRICTLY PROHIBITED.

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