0% found this document useful (0 votes)
9 views15 pages

CPL Assignment Task 5

Uploaded by

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

CPL Assignment Task 5

Uploaded by

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

CPL ASSIGNMENT TASK 5

1. Write a function to find the maximum of two numbers

#include <stdio.h>

int max(int a, int b) {

return a > b ? a : b;

int main() {

printf("Maximum: %d\n", max(5, 10));

return 0;

OUTPUT:-

Maximum: 10

2. Write a program to calculate the factorial of a number using a function

#include <stdio.h>

int factorial(int n) {

int result = 1;

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

result *= i;

return result;

int main() {

printf("Factorial: %d\n", factorial(5));

return 0;

}
OUTPUT:-

Factorial: 120

3. Create a function that takes two numbers and returns their greatest common divisor (GCD).

#include <stdio.h>

int gcd(int a, int b) {

return b == 0 ? a : gcd(b, a % b);

int main() {

printf("GCD: %d\n", gcd(20, 30));

return 0;

OUTPUT:-

GCD: 10

4. Write a function to swap two integer values using pointers.

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 5, y = 10;

swap(&x, &y);

printf("x: %d, y: %d\n", x, y);

return 0;

OUTPUT:-
x: 10, y: 5

5. Implement a recursive function to calculate the nth Fibonacci number.

#include <stdio.h>

int fibonacci(int n) {

if (n <= 1) return n;

return fibonacci(n - 1) + fibonacci(n - 2);

int main() {

printf("Fibonacci: %d\n", fibonacci(6));

return 0;

OUTPUT:-

Fibonacci: 8

6. Write a function to check if a number is prime.

#include <stdio.h>

int isPrime(int n) {

if (n <= 1) return 0;

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

if (n % i == 0)

return 0;

return 1;

int main() {

printf("Prime: %d\n", isPrime(7));

return 0;

OUTPUT:-
Prime: 1

7. Create a function that calculates the power of a number (base^exponent) using recursion

#include <stdio.h>

int power(int base, int exp) {

return exp == 0 ? 1 : base * power(base, exp - 1);

int main() {

printf("Power: %d\n", power(2, 3));

return 0;

OUTPUT:-

Power: 8

8. Write a program that includes a function to determine if a number is odd or even.

#include <stdio.h>

int isEven(int n) {

return n % 2 == 0;

int main() {

printf("Even: %d\n", isEven(4));

return 0;

OUTPUT:-

Even: 1

9. Write a function that returns the sum of two floating-point numbers

#include <stdio.h>

float sum(float a, float b) {


return a + b;

int main() {

printf("Sum: %.2f\n", sum(1.5, 2.5));

return 0;

OUTPUT:-

Sum: 4.00

10. Implement a function to check if a number is a palindrome.

#include <stdio.h>

int isPalindrome(int n) {

int reversed = 0, temp = n;

while (temp) {

reversed = reversed * 10 + temp % 10;

temp /= 10;

return n == reversed;

int main() {

printf("Palindrome: %d\n", isPalindrome(121));

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() {

printf("Sum of digits: %d\n", sumOfDigits(123));

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>

const char* checkNumber(int n) {

return n > 0 ? "Positive" : (n < 0 ? "Negative" : "Zero");

int main() {

printf("Number is: %s\n", checkNumber(-5));

return 0;

OUTPUT:-

Number is: Negative

13. Implement a function to find the least common multiple (LCM) of two numbers.

#include <stdio.h>

int gcd(int a, int b) {


return b == 0 ? a : gcd(b, a % b);

int lcm(int a, int b) {

return (a * b) / gcd(a, b);

int main() {

printf("LCM: %d\n", lcm(12, 18));

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() {

printf("Sum of squares: %d\n", sumOfSquares(3));

return 0;

OUTPUT:-

Sum of squares: 14

15. Create a function that returns the reverse of a given integer.

#include <stdio.h>

int reverse(int n) {

int reversed = 0;
while (n) {

reversed = reversed * 10 + n % 10;

n /= 10;

return reversed;

int main() {

printf("Reversed: %d\n", reverse(1234));

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>

double compoundInterest(double principal, double rate, int time) {

return principal * pow((1 + rate / 100), time);

int main() {

printf("Compound Interest: %.2f\n", compoundInterest(1000, 5, 2));

return 0;

OUTPUT:-

Compound Interest: 1102.50

17. Implement a function to convert a decimal number to binary (without using arrays or
strings).

#include <stdio.h>
void decimalToBinary(int n) {

if (n > 1) decimalToBinary(n / 2);

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

void circleProperties(double radius, double *area, double *circumference) {

*area = PI * radius * radius;

*circumference = 2 * PI * radius;

int main() {

double area, circumference;

circleProperties(5, &area, &circumference);

printf("Area: %.2f\nCircumference: %.2f\n", area, circumference);

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() {

printf("Sum of cubes: %d\n", sumOfCubes(3));

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

double cylinderVolume(double radius, double height) {

return PI * radius * radius * height;

int main() {

printf("Volume: %.2f\n", cylinderVolume(3, 5));

return 0;

OUTPUT:-

Volume: 141.37

21. Implement a function to check if a number is an Armstrong number.

#include <stdio.h>
#include <math.h>

int isArmstrong(int n) {

int sum = 0, temp = n, digits = 0;

while (temp) {

digits++;

temp /= 10;

temp = n;

while (temp) {

sum += pow(temp % 10, digits);

temp /= 10;

return sum == n;

int main() {

printf("Armstrong: %d\n", isArmstrong(153));

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;

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

if (n % i == 0)

return 0;

return 1;

}
int sumOfPrimes(int n) {

int sum = 0;

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

if (isPrime(i))

sum += i;

return sum;

int main() {

printf("Sum of primes: %d\n", sumOfPrimes(10));

return 0;

OUTPUT:-

Sum of primes: 17

23. Create a function to find the greatest of three numbers.

#include <stdio.h>

int greatest(int a, int b, int c) {

return (a > b && a > c) ? a : (b > c ? b : c);

int main() {

printf("Greatest: %d\n", greatest(3, 7, 5));

return 0;

OUTPUT:-

Greatest: 7

24. Implement a function to find the factorial of a number iteratively

#include <stdio.h>

int factorial(int n) {
int result = 1;

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

result *= i;

return result;

int main() {

printf("Factorial: %d\n", factorial(6));

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 diff = t1 > t2 ? t1 - t2 : t2 - t1;

*dh = diff / 60;

*dm = diff % 60;

int main() {

int dh, dm;

timeDifference(5, 30, 8, 20, &dh, &dm);

printf("Difference: %d hours and %d minutes\n", dh, dm);

return 0;

OUTPUT:-

Difference: 2 hours and 50 minutes


26. Create a function that returns the minimum of two floating-point numbers.

#include <stdio.h>

float minimum(float a, float b) {

return a < b ? a : b;

int main() {

printf("Minimum: %.2f\n", minimum(3.5, 2.7));

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>

float simpleInterest(float principal, float rate, float time) {

return (principal * rate * time) / 100;

int main() {

printf("Simple Interest: %.2f\n", simpleInterest(1000, 5, 2));

return 0;

OUTPUTl:-

Simple Interest: 100.00

You might also like