CPL Assignment Task 5
CPL Assignment Task 5
#include <stdio.h>
return a > b ? a : b;
int main() {
return 0;
OUTPUT:-
Maximum: 10
#include <stdio.h>
int factorial(int n) {
int result = 1;
result *= i;
return result;
int main() {
return 0;
}
OUTPUT:-
Factorial: 120
3. Create a function that takes two numbers and returns their greatest common divisor (GCD).
#include <stdio.h>
int main() {
return 0;
OUTPUT:-
GCD: 10
#include <stdio.h>
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
swap(&x, &y);
return 0;
OUTPUT:-
x: 10, y: 5
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1) return n;
int main() {
return 0;
OUTPUT:-
Fibonacci: 8
#include <stdio.h>
int isPrime(int n) {
if (n <= 1) return 0;
if (n % i == 0)
return 0;
return 1;
int main() {
return 0;
OUTPUT:-
Prime: 1
7. Create a function that calculates the power of a number (base^exponent) using recursion
#include <stdio.h>
int main() {
return 0;
OUTPUT:-
Power: 8
#include <stdio.h>
int isEven(int n) {
return n % 2 == 0;
int main() {
return 0;
OUTPUT:-
Even: 1
#include <stdio.h>
int main() {
return 0;
OUTPUT:-
Sum: 4.00
#include <stdio.h>
int isPalindrome(int n) {
while (temp) {
temp /= 10;
return n == reversed;
int main() {
return 0;
OUTPUT:-
Palindrome: 1
11. Write a function that calculates the sum of the digits of a given number.
#include <stdio.h>
int sumOfDigits(int n) {
int sum = 0;
while (n) {
sum += n % 10;
n /= 10;
return sum;
int main() {
return 0;
OUTPUT:-
Sum of digits: 6
12. Write a function that accepts a number and returns whether it is positive, negative, or zero.
#include <stdio.h>
int main() {
return 0;
OUTPUT:-
13. Implement a function to find the least common multiple (LCM) of two numbers.
#include <stdio.h>
int main() {
return 0;
OUTPUT:-
LCM: 36
14. Write a program to calculate the sum of squares of the first n natural numbers using a
function.
#include <stdio.h>
int sumOfSquares(int n) {
return (n * (n + 1) * (2 * n + 1)) / 6;
int main() {
return 0;
OUTPUT:-
Sum of squares: 14
#include <stdio.h>
int reverse(int n) {
int reversed = 0;
while (n) {
n /= 10;
return reversed;
int main() {
return 0;
OUTPUT:-
Reversed: 4321
16. Write a function to compute the compound interest for a given principal, rate, and time.
#include <stdio.h>
#include <math.h>
int main() {
return 0;
OUTPUT:-
17. Implement a function to convert a decimal number to binary (without using arrays or
strings).
#include <stdio.h>
void decimalToBinary(int n) {
printf("%d", n % 2);
int main() {
printf("Binary: ");
decimalToBinary(10);
printf("\n");
return 0;
OUTPUT:-
Binary: 1010
18. Write a program that uses a function to calculate the area and circumference of a circle,
given its radius.
#include <stdio.h>
#define PI 3.14159
*circumference = 2 * PI * radius;
int main() {
return 0;
OUTPUT:-
Area: 78.54
Circumference: 31.42
19. Write a function to calculate the sum of cubes of the first n natural numbers
#include <stdio.h>
int sumOfCubes(int n) {
return (n * n * (n + 1) * (n + 1)) / 4;
int main() {
return 0;
OUTPUT:-
Sum of cubes: 36
20. Create a function that takes the radius and height of a cylinder and returns its volume.
#include <stdio.h>
#define PI 3.14159
int main() {
return 0;
OUTPUT:-
Volume: 141.37
#include <stdio.h>
#include <math.h>
int isArmstrong(int n) {
while (temp) {
digits++;
temp /= 10;
temp = n;
while (temp) {
temp /= 10;
return sum == n;
int main() {
return 0;
OUTPUT:-
Armstrong: 1
22. Write a function to find the sum of all prime numbers less than a given number.
#include <stdio.h>
int isPrime(int n) {
if (n <= 1) return 0;
if (n % i == 0)
return 0;
return 1;
}
int sumOfPrimes(int n) {
int sum = 0;
if (isPrime(i))
sum += i;
return sum;
int main() {
return 0;
OUTPUT:-
Sum of primes: 17
#include <stdio.h>
int main() {
return 0;
OUTPUT:-
Greatest: 7
#include <stdio.h>
int factorial(int n) {
int result = 1;
result *= i;
return result;
int main() {
return 0;
OUTPUT:-
Factorial: 720
25. Write a function that calculates the difference between two times (hours and minutes).
#include <stdio.h>
void timeDifference(int h1, int m1, int h2, int m2, int *dh, int *dm) {
int t1 = h1 * 60 + m1;
int t2 = h2 * 60 + m2;
int main() {
return 0;
OUTPUT:-
#include <stdio.h>
return a < b ? a : b;
int main() {
return 0;
OUTPUT:-
Minimum: 2.70
27. Write a program that uses a function to calculate the simple interest for a given principal,
rate, and time.
#include <stdio.h>
int main() {
return 0;
OUTPUTl:-