Lab Activity5 Vinay Kapoor
Lab Activity5 Vinay Kapoor
1. Write a function to swap two numbers using a third variable and call it in the main
function.
Solution:
#include<stdio.h>
int c=*x;
*x=*y;
*y=c;}
int main()
int num1,num2;
scanf(“%d%d”,&num1,&num2);
swap(&num1,&num2);
return 0;
}
Output:
2. Write a function to calculate the factorial of a number using recursion.
Solution:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
int main() {
int num;
scanf("%d", &num);
fact= factorial(num);
return 0;
Output:
3. Write a function to find the greatest common divisor (GCD) of two numbers using
recursion.
Solution:
#include <stdio.h>
if (b == 0)
return a;
int main() {
result=gcd(num1,num2);
return 0;
}
4. Write a function to reverse an integer number and return it to the calling function.
Solution:
#include <stdio.h>
int reversed = 0;
while (num != 0) {
num /= 10;
return reversed;
int main() {
scanf("%d", &num);
reversed = reverseNumber(num);
return 0;
Output:
5. Write a function to find the sum of the first n natural numbers using a loop inside the
function.
Solution:
#include <stdio.h>
int sumOfNaturalNumbers(int n) {
int sum = 0;
sum += i;
return sum;
int main() {
int n, sum;
scanf("%d", &n);
if (n < 1) {
} else {
sum = sumOfNaturalNumbers(n);
return 0;
Output:
6. Write a program with a function that takes an integer as input and returns whether it is
prime or not.
Solution:
#include <stdio.h>
int isPrime(int n) {
if (n <= 1)
return 0;
if (n % i == 0)
return 0;
return 1;
int main() {
int num;
scanf("%d", &num);
if (isPrime(num)) {
} else {
return 0;
Output:
7. Write a function to calculate the nth Fibonacci number using recursion.
Solution:
#include <stdio.h>
int fibonacci(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
int main() {
int n;
scanf("%d", &n);
if (n < 0) {
} else {
return 0;
Output:
8. Write a function that takes two integer arrays and their size as input and returns an
Solution:
#include <stdio.h>
int main() {
int size;
scanf("%d", &size);
scanf("%d", &arr1[i]);
scanf("%d", &arr2[i]);
printf("\n");
return 0;
Output:
9. Write a program that demonstrates the concept of function overloading by using
multiple functions with the same name to calculate the area of a circle and a
rectangle.
Solution:
#include <stdio.h>
#define PI 3.14159
int main() {
scanf("%lf", &radius);
Output:
10. Write a program to implement a simple calculator using functions for addition,
Solution:
#include <stdio.h>
return a + b;
return a - b;
return a * b;
if (b != 0) {
return (float)a / b;
} else {
return 0;
int main() {
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
printf("Invalid choice!\n");
return 0;
Output:
11. Write a program to swap two numbers using pointers without using a third variable.
Solution:
#include <stdio.h>
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
int main() {
swap(&num1, &num2);
return 0;
Output:
13. Write a program that uses a pointer to a function to perform different arithmetic
Solution:
#include <stdio.h>
return a + b;
return a - b;
return a * b;
if (b != 0) {
return (float)a / b;
} else {
return 0;
int main() {
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
printf("Invalid choice!\n");
return 0;
}
Output:
14. Write a function that converts Celsius to Fahrenheit and another that converts
Solution:
#include <stdio.h>
int main() {
int choice;
printf("Choose a conversion:\n");
scanf("%d", &choice);
if (choice == 1) {
scanf("%f", &temperature);
result = celsiusToFahrenheit(temperature);
} else if (choice == 2) {
scanf("%f", &temperature);
result = fahrenheitToCelsius(temperature);
printf("%.2f Fahrenheit = %.2f Celsius\n", temperature, result);
} else {
printf("Invalid choice!\n");
return 0;
Output:
15. Write a function to calculate simple interest given principal, rate, and time. Call the
function in the main program to display the interest for different values.
Solution:
#include <stdio.h>
int main() {
scanf("%f", &principal);
scanf("%f", &rate);
scanf("%f", &time);
return 0;
Output:
16. Write a function that takes an integer and returns the sum of its digits.
Solution:
#include <stdio.h>
int sum = 0;
while (num != 0) {
num /= 10;
return sum;
int main() {
int num;
scanf("%d", &num);
return 0;
Output:
17. Write a function to calculate the factorial of a number using loops.
Solution:
#include <stdio.h>
int factorial(int n) {
int result = 1;
result *= i;
return result;
int main() {
int num;
scanf("%d", &num);
if (num < 0) {
} else {
return 0;
}
18. Write a function to check whether a given number is a palindrome (the same forward
and backward).
Solution:
#include <stdio.h>
while (num != 0) {
num /= 10;
if (originalNum == reversedNum) {
return 1; // Palindrome
} else {
int main() {
int num;
scanf("%d", &num);
if (isPalindrome(num)) {
} else {
printf("%d is not a palindrome.\n", num);
return 0;
Output:
19. Write a function that takes a student’s score as input and returns their grade based
solution:
#include <stdio.h>
return 'A';
return 'B';
return 'C';
return 'D';
} else {
return 'F';
int main() {
int score;
scanf("%d", &score);
} else {
}
return 0;
Output:
20. Write a function to calculate the Least Common Multiple (LCM) of two numbers.
Solution:
#include <stdio.h>
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
return a;
int main() {
return 0;
Output:
21. Write a function that returns the sum of squares of all numbers up to n (e.g., for n = 3,
Solution:
#include <stdio.h>
int sumOfSquares(int n) {
int sum = 0;
sum += i * i;
return sum;
int main() {
int n;
scanf("%d", &n);
return 0;
Output:
22. Write a function that takes an integer and returns the number of digits in it.
Solution:
#include <stdio.h>
int count = 0;
if (num == 0) {
return 1;
while (num != 0) {
num /= 10;
count++;
return count;
int main() {
int num;
scanf("%d", &num);
return 0;
Output:
23. Write a C program that declares an integer variable and a pointer to that integer.
Assign a value to the integer and use the pointer to display the value and address of
the integer.
Solution:
#include <stdio.h>
int main() {
return 0;
Output:
24. Write a program that declares two integer variables and a pointer to an integer.
Assign the address of one variable to the pointer and modify the value of the variable
through the pointer. Display the values before and after modification.
Solution:
#include <stdio.h>
int main() {
int *ptr;
printf("Before modification:\n");
printf("\nAfter modification:\n");
return 0;
Output:
25. Create a C program that initializes an integer variable and a pointer pointing to the
variable. Use pointer arithmetic to increment the pointer and display the address
Solution:
#include <stdio.h>
int main() {
return 0;
Output:
26. Write a program that demonstrates the concept of a pointer to a pointer. Declare an
integer variable, a pointer to that integer, and a pointer to the pointer. Use these to
Solution:
#include <stdio.h>
int main() {
return 0;
Output:
27. Write a C program where a function takes an integer pointer as an argument. The
function should modify the value of the integer through the pointer, and the program
should display the value of the integer before and after calling the function.
Solution:
#include <stdio.h>
*ptr = 50;
int main() {
modifyValue(&num);
return 0;
Output:
28. Create a C program where a function returns a pointer to an integer. The program
should declare an integer variable, pass its address to the function, and the function
should return a pointer to that variable. Use the pointer to display the integer's value.
Solution:
#include <stdio.h>
return ptr;
int main() {
return 0;
Output:
29. Implement a program that defines a function to calculate the square of an integer
using pointers. The function should take a pointer to an integer as an argument and
modify the integer to its squared value. Display the result using the original variable in
Solution:
#include <stdio.h>
int main() {
int num = 5;
square(&num);
return 0;
Output:
30. Write a C program that demonstrates the use of a pointer to a function. Define a
function that adds two integers. In the main function, declare a pointer to the function
and use it to call the function, passing two integers. Display the result of the function
call.
Solution:
#include <stdio.h>
return a + b;
int main() {
funcPtr = add;
return 0;
Output: