0% found this document useful (0 votes)
24 views8 pages

Cycle 8 Test

Cycle About the c language in sreenidhi for 1st year cycle 8

Uploaded by

abhinaybandi412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Cycle 8 Test

Cycle About the c language in sreenidhi for 1st year cycle 8

Uploaded by

abhinaybandi412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SNIST_C_COD_Cycle 8

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.

Function Specifications: void checkPrime(int n)


Input Format
The input consists of a single integer n.
Output Format
The output prints "n is a prime number" If n is prime.
Otherwise, prints "n is not a prime number".

Refer to the sample output for the formatting specifications.


Constraints
1 ≤ n ≤ 105
Sample Input
2

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;
}

for (int i = 2; i * i <= n; i++) {


if (n % i == 0) {
printf("%d is not a prime number", n);
return;
}
}

printf("%d is a prime number", n);


}
int main() {
int n;
scanf("%d", &n);
checkPrime(n);
return 0;
}

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.

Function specifications: double power(double x, int n)


Input Format
The first line contains a double value x.
The second line contains a non-negative integer n.
Output Format
The output prints "x.00 raised to the power of n is: y.00", where y is the calculated result
formatted to two decimal places.

Refer to the sample output for the formatting specifications.


Constraints
0.0 ≤ x ≤ 10.0
0 ≤ n ≤ 10
Sample Input
2.0
3
Sample Output
2.00 raised to the power of 3 is: 8.00
Sample Input
5.0
0

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.

Function specification: int fibonacci(int n)


Input Format
The input consists of a single integer n.
Output Format
The output prints a single integer representing the n-th Fibonacci number.

Refer to the sample output for the formatting specifications.


Constraints
0 ≤ n ≤ 30
Sample Input
0

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;

for (int i = 2; i <= n; i++) {


c = a + b;
a = b;
b = c;
}

return b;
}
int main() {
int n;
scanf("%d", &n);
printf("%d", fibonacci(n));
return 0;
}

Q4.Problem Statement

Write a program to calculate the factorial of a non-negative integer n using recursion.

Function Specifications: int factorial(int n)


Input Format
The input consists of a single integer N, representing the number for which the factorial is to be
calculated.
Output Format
The output prints a single integer representing the factorial of the given number N.

Refer to the sample output for the formatting specifications.


Constraints
In this scenario, the given test cases will fall under the following constraints:
0 ≤ n ≤ 12
Sample Input
5
Sample Output
120

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)

Function Specifications: int combinations(int n, int r)


Input Format
The first line contains an integer n.
The second line contains an integer r.
Output Format
The output prints "Number of combinations (C (n, r)) is: x" where x is the computed number of
combinations.

Refer to the sample output for the formatting specifications.


Constraints
1 ≤ n ≤ 30
1≤r≤n
Sample Input
5
2
Sample Output
Number of combinations (C(5, 2)) is: 10
Sample Input Sample Output
4
2
Number of combinations (C(4, 2)) is: 6

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);

printf("Number of combinations (C(%d, %d)) is: %lld\n", n, r, combinations(n,


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.

Function specifications: int gcd(int a, int b)


Input Format
The input consists of a single line containing two space-separated integers a and b.
Output Format
The output prints a single integer representing the greatest common divisor (GCD) of a and b.

Refer to the sample output for the formatting specifications.


Constraints
1 ≤ a, b ≤ 106
Sample Input
8 12
Sample Output
4

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.

Function Specifications: int lcm(int a, int b)


Input Format
The input consists of a single line containing two space-separated integers a and b.
Output Format
The output prints a single integer representing the least common multiple (LCM) of a and b.

Refer to the sample output for the formatting specifications.


Constraints
In the given scenario, the test cases will fall under the following constraints:
1 ≤ a, b ≤ 104
Sample Input
4 5
Sample Output
20
Sample Input
6 8
Sample Output
24

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;
}

You might also like