Cycle 8 Test
Cycle 8 Test
Q1.Problem Statement
Write a function that takes an integer n as argument and returns 1 if it is a prime number and 0
otherwise.
Sample Output
2 is a prime number
Sample Input
10
Sample Output
10 is not a prime number
Sample Input
10007
Sample Output
10007 is a prime number
Solution
#include <stdio.h>
void checkPrime(int n) {
if (n <= 1) {
printf("%d is not a prime number", n);
return;
}
Q2.Problem Statement
Write a C program that calculates the power of a real number raised to a positive integer
exponent. The program should take a real number x and a positive integer n as inputs, and
compute xn .The result should be displayed with two decimal places.
Sample Output
5.00 raised to the power of 0 is: 1.00
Sample Input
10.0
2
Sample Output
10.00 raised to the power of 2 is: 100.00
Solution
#include <stdio.h>
double power(double x, int n) {
double result = 1.0;
for (int i = 0; i < n; i++) {
result *= x;
}
return result;
}
int main() {
double x;
int n;
scanf("%lf", &x);
scanf("%d", &n);
printf("%.2lf raised to the power of %d is: %.2lf\n", x, n, power(x, n));
return 0;
}
Q3.Problem Statement
Write a function that takes a positive integer n as an argument and returns the nth Fibonacci
number.
Sample Output
0
Sample Input
1
Sample Output
1
Sample Input
10
Sample Output
55
Solution
#include <stdio.h>
int fibonacci(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
int a = 0;
int b = 1;
int c;
return b;
}
int main() {
int n;
scanf("%d", &n);
printf("%d", fibonacci(n));
return 0;
}
Q4.Problem Statement
Sample Input
6
Sample Output
720
Sample Input
0
Sample Output
1
Solution
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
scanf("%d", &num);
int result = factorial(num);
printf("%d",result);
return 0;
}
Q5.Problem Statement
Write a program that calculates the number of combinations of n items taken r at a time, denoted
as C(n,r) using recursion. The combination formula is used to determine how many ways we can
choose r items from n distinct items, where the order of selection does not matter.
Formula: C(n,r)=C(n−1,r−1)+C(n−1,r)
Solution
#include <stdio.h>
int combinations(int n, int r) {
// Base cases
if (r == 0 || r == n) {
return 1;
}
return combinations(n - 1, r - 1) + combinations(n - 1, r);
}
int main() {
int n, r;
scanf("%d", &n);
scanf("%d", &r);
return 0;
}
Q6.Problem Statement
Write a program to find the Greatest Common Divisor (GCD) of two integers using a recursive
function. The GCD of two integers is the largest positive integer that divides both of the numbers
without leaving a remainder.
Sample Input
10 25
Sample Output
5
Solution
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d",gcd(a, b));
return 0;
}
Q7.Problem Statement
Write a program to find Least Common Multiple of two integers using recursion.
Solution
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d",lcm(a, b));
return 0;
}