Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C PROGRAMMING Q1. FACTORIAL factorial(1) 1 factorial(2) 2 factorial(3) 6 factorial(8) 40320 factorial(12) 479001600 Code: int factorial(int n) { return n == 0 ?

IN C PROGRAMMING

Q1. FACTORIAL

factorial(1) 1

factorial(2) 2

factorial(3) 6

factorial(8) 40320

factorial(12) 479001600

Code:

int factorial(int n) {

return n == 0 ? 1 : n * factorial(n - 1);

}

Q2. GCD (Great Common Divisor)

Computing the recurrence relation for x = 27 and y = 9

Code:

int gcd(int x, int y) {

return y == 0 ? x : gcd(y, x % y);

}

Q3. Fibonacci

fibonacci(0) 0

fibonacci(1) 1

fibonacci(2) 1

fibonacci(3) 2

fibonacci(4) 3

fibonacci(5) 5

Code:

int fibonacci(int n) {

return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);

}

Q4. Bunny Ears

bunnyEars(0) 0

bunnyEars(1) 2

bunnyEars(2) 4

bunnyEars(3) 6

bunnyEars(234) 468

Code:

int bunnyEars(int bunnies) { return bunnies == 0 ? 0 : 2 + bunnyEars(bunnies - 1); }

Q5. Funny Ears

funnyEars(0) 0

funnyEars(1) 2

funnyEars(2) 5

funnyEars(3) 7

funnyEars(4) 10

funnyEars(9) 22

Code:

int funnyEars(int funnies) { if (funnies == 0) return 0;

if (funnies % 2 == 0) return 3 + funnyEars(funnies - 1);

return 2 + funnyEars(funnies - 1);

}

Q6. Triangle

triangle(0) 0

triangle(1) 1

triangle(2) 3

triangle(3) 6

triangle(4) 10

triangle(7) 28

Code:

int triangle(int rows) {

return rows <= 1 ? rows : rows + triangle(rows - 1);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

How do retained earnings differ from other sources of financing?

Answered: 1 week ago

Question

=+ (b) Show that X ,, - p X if and only if d( X ,, X) ->0.

Answered: 1 week ago