IN C PROGRAMMING Q1. FACTORIAL factorial(1) 1 factorial(2) 2 factorial(3) 6 fact
ID: 3874655 • Letter: I
Question
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:
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);
}
factorial(1) 1
factorial(2) 2
factorial(3) 6
factorial(8) 40320
factorial(12) 479001600
Explanation / Answer
#include <stdio.h>
int factorial(int n)
{
return n == 0 ? 1 : n * factorial(n - 1);
}
int main(void)
{
int n;
scanf("%d",&n);//Taking input from user
int r=factorial(n);
printf("Factorial of %d is :%d ",n,r);//printing the result
return 0;
}
Sample Input :
5
Sample Output:
Factorial of 5 is :120
#include <stdio.h>
int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
int main(void)
{
int a,b;
scanf("%d %d ",&a,&b);//Taking input from user
printf("GCD of %d and %d is : %d ",a,b,gcd(a,b));//printing the result
return 0;
}
Sample Input :
27
9
Sample Output:
GCD of 27 and 9 is :9
#include <stdio.h>
int fibonacci(int n) {
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
int main(void)
{
int n;
scanf("%d",&n);//Taking input from user
printf("fibonacci of %d is :%d ",n,fibonacci(n));//printing the result
return 0;
}
Sample Input :
5
Sample Output:
Fibonacci of 5 is :5
#include <stdio.h>
int bunnyEars(int bunnies)
{
return bunnies == 0 ? 0 : 2 + bunnyEars(bunnies - 1); }
int main(void)
{
int n;
scanf("%d",&n);
printf("Result of bunnyEars for %d is :%d ",n,bunnyEars(n));//printing the result
return 0;
}
Sample Input :
234
Sample Output:
Result of bunnyEars for 234 is :468
#include <stdio.h>
int funnyEars(int funnies)
{
if (funnies == 0) return 0;
if (funnies % 2 == 0) return 3 + funnyEars(funnies - 1);
return 2 + funnyEars(funnies - 1);
}
int main(void)
{
int n;
scanf("%d",&n);//Taking input from user
printf("Result of funnyEars for %d is :%d ",n,funnyEars(n));//printing the result
return 0;
}
Sample Input :
9
Sample Output:
Result of funnyEars for 9 is :22
#include <stdio.h>
int triangle(int rows)
{
return rows <= 1 ? rows : rows + triangle(rows - 1);
}
int main(void)
{
int n;
scanf("%d",&n);//Taking input from user
printf("Result of Triangle for %d row is :%d ",n,triangle(n));//printing the result
return 0;
}
Sample Input :
7
Sample Output:
Result of Triangle for 7 row is :28