0% found this document useful (0 votes)
14 views

Lab Activity5 Vinay Kapoor

Uploaded by

vinaykapoorin
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)
14 views

Lab Activity5 Vinay Kapoor

Uploaded by

vinaykapoorin
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/ 56

{QUESTIONS FOR LAB ACTIVITY 5}

1. Write a function to swap two numbers using a third variable and call it in the main

function.

Solution:

#include<stdio.h>

void swap(int *x,int *y){

int c=*x;

*x=*y;

*y=c;}

int main()

int num1,num2;

printf(“enter two numbers”);

scanf(“%d%d”,&num1,&num2);

printf(“\n before swapping:”);

printf(“\n a=%d\n b=%d”,num1,num2);

swap(&num1,&num2);

printf(“\n after swapping:”);

printf(“\n a=%d\n b=%d”,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;

return n * factorial(n - 1);

int main() {

int num;

long int fact;

printf("Enter a number: ");

scanf("%d", &num);

fact= factorial(num);

printf("Factorial of %d is %ld\n", num,fact);

return 0;

Output:
3. Write a function to find the greatest common divisor (GCD) of two numbers using

recursion.

Solution:

#include <stdio.h>

int gcd(int a, int b) {

if (b == 0)

return a;

return gcd(b, a % b);

int main() {

int num1, num2,result;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

result=gcd(num1,num2);

printf("GCD of %d and %d is %d\n", num1, num2, result);

return 0;

}
4. Write a function to reverse an integer number and return it to the calling function.

Solution:

#include <stdio.h>

int reverseNumber(int num) {

int reversed = 0;

while (num != 0) {

reversed = reversed * 10 + num % 10;

num /= 10;

return reversed;

int main() {

int num, reversed;

printf("Enter an integer: ");

scanf("%d", &num);

reversed = reverseNumber(num);

printf("Reversed number: %d\n", reversed);

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;

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

sum += i;

return sum;

int main() {

int n, sum;

printf("Enter a positive integer: ");

scanf("%d", &n);

if (n < 1) {

printf("Please enter a positive integer greater than 0.\n");

} else {

sum = sumOfNaturalNumbers(n);

printf("The sum of the first %d natural numbers is: %d\n", n, sum);

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;

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

if (n % i == 0)

return 0;

return 1;

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

if (isPrime(num)) {

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

} else {

printf("%d is not a prime number.\n", num);

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;

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

int main() {

int n;

printf("Enter the position (n) to find the Fibonacci number: ");

scanf("%d", &n);

if (n < 0) {

printf("Fibonacci sequence is not defined for negative numbers.\n");

} else {

printf("The %dth Fibonacci number is: %d\n", n, fibonacci(n));

return 0;

Output:
8. Write a function that takes two integer arrays and their size as input and returns an

array containing their element-wise sum.

Solution:

#include <stdio.h>

void elementWiseSum(int arr1[], int arr2[], int sum[], int size) {

for (int i = 0; i < size; i++) {

sum[i] = arr1[i] + arr2[i];

int main() {

int size;

printf("Enter the size of the arrays: ");

scanf("%d", &size);

int arr1[size], arr2[size], sum[size];

printf("Enter elements of the first array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr1[i]);

printf("Enter elements of the second array:\n");

for (int i = 0; i < size; i++) {

scanf("%d", &arr2[i]);

elementWiseSum(arr1, arr2, sum, size);


printf("Element-wise sum of the arrays:\n");

for (int i = 0; i < size; i++) {

printf("%d ", sum[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

// Function to calculate the area of a circle

double areaOfCircle(double radius) {

return PI * radius * radius;

// Function to calculate the area of a rectangle

double areaOfRectangle(double length, double width) {

return length * width;

int main() {

double radius, length, width;

// Input and calculation for the area of the circle

printf("Enter the radius of the circle: ");

scanf("%lf", &radius);

printf("Area of the circle: %.2lf\n", areaOfCircle(radius));

// Input and calculation for the area of the rectangle

printf("Enter the length and width of the rectangle: ");

scanf("%lf %lf", &length, &width);

printf("Area of the rectangle: %.2lf\n", areaOfRectangle(length, width));


return 0;

Output:
10. Write a program to implement a simple calculator using functions for addition,

subtraction, multiplication, and division.

Solution:

#include <stdio.h>

int add(int a, int b) {

return a + b;

int subtract(int a, int b) {

return a - b;

int multiply(int a, int b) {

return a * b;

float divide(int a, int b) {

if (b != 0) {

return (float)a / b;

} else {

printf("Error! Division by zero.\n");

return 0;

int main() {

int num1, num2, choice;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);


printf("Choose an operation: \n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Result: %d\n", add(num1, num2));

break;

case 2:

printf("Result: %d\n", subtract(num1, num2));

break;

case 3:

printf("Result: %d\n", multiply(num1, num2));

break;

case 4:

printf("Result: %.2f\n", divide(num1, num2));

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>

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

*a = *a + *b;

*b = *a - *b;

*a = *a - *b;

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("Before swap: num1 = %d, num2 = %d\n", num1, num2);

swap(&num1, &num2);

printf("After swap: num1 = %d, num2 = %d\n", num1, num2);

return 0;

Output:
13. Write a program that uses a pointer to a function to perform different arithmetic

operations based on user input.

Solution:

#include <stdio.h>

int add(int a, int b) {

return a + b;

int subtract(int a, int b) {

return a - b;

int multiply(int a, int b) {

return a * b;

float divide(int a, int b) {

if (b != 0) {

return (float)a / b;

} else {

printf("Error! Division by zero.\n");

return 0;

int main() {

int num1, num2, choice;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);


printf("Choose an operation: \n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Result: %d\n", add(num1, num2));

break;

case 2:

printf("Result: %d\n", subtract(num1, num2));

break;

case 3:

printf("Result: %d\n", multiply(num1, num2));

break;

case 4:

printf("Result: %.2f\n", divide(num1, num2));

break;

default:

printf("Invalid choice!\n");

return 0;

}
Output:
14. Write a function that converts Celsius to Fahrenheit and another that converts

Fahrenheit to Celsius. Use these functions in the main program.

Solution:

#include <stdio.h>

float celsiusToFahrenheit(float celsius) {

return (celsius * 9/5) + 32;

float fahrenheitToCelsius(float fahrenheit) {

return (fahrenheit - 32) * 5/9;

int main() {

float temperature, result;

int choice;

printf("Choose a conversion:\n");

printf("1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius\n");

scanf("%d", &choice);

if (choice == 1) {

printf("Enter temperature in Celsius: ");

scanf("%f", &temperature);

result = celsiusToFahrenheit(temperature);

printf("%.2f Celsius = %.2f Fahrenheit\n", temperature, result);

} else if (choice == 2) {

printf("Enter temperature in Fahrenheit: ");

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>

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

return (principal * rate * time) / 100;

int main() {

float principal, rate, time, interest;

printf("Enter the principal amount: ");

scanf("%f", &principal);

printf("Enter the rate of interest: ");

scanf("%f", &rate);

printf("Enter the time in years: ");

scanf("%f", &time);

interest = calculateSimpleInterest(principal, rate, time);

printf("The simple interest is: %.2f\n", interest);

return 0;

Output:
16. Write a function that takes an integer and returns the sum of its digits.

Solution:

#include <stdio.h>

int sumOfDigits(int num) {

int sum = 0;

while (num != 0) {

sum += num % 10;

num /= 10;

return sum;

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

printf("The sum of the digits is: %d\n", sumOfDigits(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;

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

result *= i;

return result;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;
}
18. Write a function to check whether a given number is a palindrome (the same forward

and backward).

Solution:

#include <stdio.h>

int isPalindrome(int num) {

int originalNum = num;

int reversedNum = 0, remainder;

while (num != 0) {

remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;

num /= 10;

if (originalNum == reversedNum) {

return 1; // Palindrome

} else {

return 0; // Not a palindrome

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (isPalindrome(num)) {

printf("%d is a palindrome.\n", 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

on the score (A, B, C, D, or F).

solution:

#include <stdio.h>

char getGrade(int score) {

if (score >= 90) {

return 'A';

} else if (score >= 80) {

return 'B';

} else if (score >= 70) {

return 'C';

} else if (score >= 60) {

return 'D';

} else {

return 'F';

int main() {

int score;

printf("Enter the student's score: ");

scanf("%d", &score);

if (score < 0 || score > 100) {

printf("Invalid score! Please enter a score between 0 and 100.\n");

} else {

char grade = getGrade(score);

printf("The student's grade is: %c\n", grade);

}
return 0;

Output:
20. Write a function to calculate the Least Common Multiple (LCM) of two numbers.

Solution:

#include <stdio.h>

int gcd(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

int lcm(int a, int b) {

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

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("The LCM of %d and %d is: %d\n", num1, num2, lcm(num1, num2));

return 0;

Output:
21. Write a function that returns the sum of squares of all numbers up to n (e.g., for n = 3,

the result is 1^2 + 2^2 + 3^2 = 14).

Solution:

#include <stdio.h>

int sumOfSquares(int n) {

int sum = 0;

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

sum += i * i;

return sum;

int main() {

int n;

printf("Enter a number: ");

scanf("%d", &n);

printf("The sum of squares up to %d is: %d\n", n, sumOfSquares(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 countDigits(int num) {

int count = 0;

if (num == 0) {

return 1;

while (num != 0) {

num /= 10;

count++;

return count;

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

printf("The number of digits in %d is: %d\n", num, countDigits(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() {

int num = 10;

int *ptr = &num;

printf("Value of num: %d\n", num);

printf("Address of num: %p\n", (void*)&num);

printf("Value of num using pointer: %d\n", *ptr);

printf("Address of num using pointer: %p\n", (void*)ptr);

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 num1 = 5, num2 = 10;

int *ptr;

printf("Before modification:\n");

printf("Value of num1: %d\n", num1);

printf("Value of num2: %d\n", num2);

ptr = &num1; // Assign the address of num1 to ptr

*ptr = 20; // Modify the value of num1 through the pointer

printf("\nAfter modification:\n");

printf("Value of num1: %d\n", num1);

printf("Value of num2: %d\n", num2);

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

before and after the increment.

Solution:

#include <stdio.h>

int main() {

int num = 10;

int *ptr = &num;

printf("Before incrementing pointer:\n");

printf("Address of num: %p\n", (void*)ptr);

printf("Value of num: %d\n", *ptr);

ptr++; // Increment the pointer to point to the next memory location

printf("\nAfter incrementing pointer:\n");

printf("Address of num (after incrementing pointer): %p\n", (void*)ptr);

printf("Value at new address (may be garbage): %d\n", *ptr);

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

display the values and addresses at each level.

Solution:

#include <stdio.h>

int main() {

int num = 25; // Declare an integer variable

int *ptr = &num; // Declare a pointer to the integer

int **ptrToPtr = &ptr; // Declare a pointer to the pointer

printf("Value of num: %d\n", num);

printf("Address of num: %p\n", (void*)&num);

printf("\nValue of ptr (address of num): %p\n", (void*)ptr);

printf("Value at ptr (value of num): %d\n", *ptr);

printf("\nValue of ptrToPtr (address of ptr): %p\n", (void*)ptrToPtr);

printf("Value at ptrToPtr (value of ptr): %p\n", (void*)*ptrToPtr);

printf("Value at the address pointed to by ptrToPtr (value of num): %d\n", **ptrToPtr);

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>

void modifyValue(int *ptr) {

*ptr = 50;

int main() {

int num = 25;

printf("Before calling the function:\n");

printf("Value of num: %d\n", num);

modifyValue(&num);

printf("\nAfter calling the function:\n");

printf("Value of num: %d\n", 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&#39;s value.

Solution:

#include <stdio.h>

int* getPointerToInt(int *ptr) {

return ptr;

int main() {

int num = 30;

int *ptr = getPointerToInt(&num);

printf("Value of num: %d\n", *ptr);

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

the main function.

Solution:

#include <stdio.h>

void square(int *ptr) {

*ptr = (*ptr) * (*ptr);

int main() {

int num = 5;

printf("Before squaring, num = %d\n", num);

square(&num);

printf("After squaring, num = %d\n", 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>

int add(int a, int b) {

return a + b;

int main() {

int (*funcPtr)(int, int);

int num1 = 10, num2 = 20;

funcPtr = add;

int result = funcPtr(num1, num2);

printf("The sum of %d and %d is: %d\n", num1, num2, result);

return 0;

Output:

You might also like