Skip to content

Latest commit

 

History

History
365 lines (234 loc) · 5.92 KB

Recursion Practice.md

File metadata and controls

365 lines (234 loc) · 5.92 KB

A. Print from 1 to N

You will be given a number N. Print from 1 to N in separate lines using recursion.

Input You will be given a number N ( 1 ≤ N ≤ 103 )

Output Print N lines contain number from 1 to N.

Example

5
1
2
3
4
5

B. Print from N to 1

You will be given a number N. Print from N to 1 separated by a single space using recursion.

Input You will be given a number N ( 1 ≤ N ≤ 103 )

Output Print from N to 1 separated by a single space.

Example

4
4 3 2 1

C. Factorial

You will be given a number N. Using recursion get the factorial of N.

Input You will be given a number N ( 1 ≤ N ≤ 20 )

Output Print the factorial of the number N.

Example

5
120

D. Max number

You will be given an array of size N. Using recursion get the maximum value in this array.

Input You will be given a number N ( 1 ≤ N ≤ 103 ). Next line will contain N numbers ( xi ≤ |109| )

Output print a maximum value in this array.

Example

5
1 -3 5 4 -6
5

E. Combination

You will be given two number n and r. Your are asked to print the value nCr using recursion.

Input The input consists of two numbers n and r ( 0 ≤ r, n ≤ 30 ).

Output Print the value nCr.

Examples

4 2
6
3 3
1

F. GCD

You will be given two numbers X and Y. You are asked to print the Greatest Common Divisor between X and Y using recursion.

Input Input will consist of two integers, X and Y ( -109 ≤ A, B ≤ 109 ) in one line. The input will not contain 2 zeros.

Output Print the GCD between X and Y.

Examples

4 16
4
5 13
1

G. Base Conversion

In this problem, you need to convert a decimal number into a binary number.

Input The first line of input contains an integer, T, which denotes the number of input you have to handle. Each of the next T lines contains an integer N (0<N<10000000).

Output For each input number you have to print the converted binary number.

Example

2
10
3
1010
11

H. Count Vowels

In this problem, you just need to count the number of vowels contain in an input string using recursion.

Input Input file contains a single line of string S. 1< |S| <200

Output Print the number of vowels contained in the input string.

Example

Data Structure Lab
6

I. Fibonacci

Given an integer N, print the value of Nth Fibonacci number using recursion. Note that, F0 = 0 and F1 = 1. Also, Fn = Fn - 1 + Fn - 2.

Input Input will consist of an integer, N (0 <= N <= 30) in one line.

Output Output will consist of 1 line: Value of Nth Fibonacci.

Example

5
5

J. Sum of a Matrix

Given two same size matrices find its summation.

Input Input will consist of two integers R and C, (0 < R, C <101). Next there will be R rows each with C integers Aijs (-100 < Aijs < 100), the first matrix. Next there will be R rows each with C integers Bij (-100 < Bij < 100), the second matrix.

Output Output will consist of Result matrix.

Example

2 3
1 2 3
4 5 6
1 3 5
7 9 11
2 5 8
11 14 17

K. Multiplication of Matrices

Given two matrices find its multiplication using recursion.

Input Input will consist of two integers Ra and Ca, (0 < Ra, Cas <101). Next there will be Ra rows each with Ca integers Aij (-100 < Aij < 100), the first matrix. Next two integers Rb and Cb, (0 < Rb, Cb <101). Next there will be Rb rows each with Cb integers Bij (-100 < Bij < 100), the first matrix. You can always assume that Ca is equal to Rb.

Output Output will consist of Result matrix.

2 2
1 2
2 1
2 2
3 4
4 3
11 10
10 11

L. Print Even Indices

Given an integer N, and N integers, print the numbers in even indices in reverse order using recursion. Assume 0-based indexing.

Input Input will consist of an integer, N (1 <= N <= 103) in the first line. The next line contains N integers Xi (Xi <= |109|).

Output The output will consist of numbers in even indices in reverse order

Examples

4
1 4 2 7
2 1
7
1 5 8 2 3 9 11
11 3 8 1

M. Sum of an Array using Recursion

Given an integer N, and N integers, find the sum of the N integers using recursion.

Input Input will consist of an integer, N (1 <= N <= 103) in the first line. The next line contains N integers Xi (Xi <= |109|).

Output The output will consist of 1 line: Value of sum.

Examples

4
1 4 2 7
14
4
5 5 5 5
20

N. Print Digits using Recursion

In this problem, you need to separate and print the digits of a given input number using recursion. You are not allowed to input the integer number as a string.

Input The first line of input contains an integer, T, which denotes the number of input you have to handle. Each of the next T lines contains an integer N (0<=N<=10000000).

Output For each input number, you have to print the digits separated by space.

Example

3
121
39
123456
1 2 1
3 9
1 2 3 4 5 6