0% found this document useful (0 votes)
16 views9 pages

EXP 10 PPS Lab

Uploaded by

hod cse
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)
16 views9 pages

EXP 10 PPS Lab

Uploaded by

hod cse
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/ 9

EX.

NO:10 FUNCTIONS: PASSING PARAMETERS BY (VALUE, REFERENCE),


RECURSION

1.Write a program in C to swap two numbers using a function.


Aim
To write a C Program to swap two numbers using a function.
Algorithm
1. Start.
2. Input two numbers, n1 and n2.
3. Define a function swap() that takes two arguments:
a. Inside the function, swap the values of the two numbers.
4. Call the swap() function with n1 and n2 as arguments.
5. Display the values of n1 and n2 before and after swapping.
6. End.

Program

#include <stdio.h>
// Function to swap two numbers
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main() {
int n1, n2;
// Input the two numbers
printf("Input 1st number: ");
scanf("%d", &n1);
printf("Input 2nd number: ");
scanf("%d", &n2);
// Display values before swapping
printf("Before swapping: n1 = %d, n2 = %d\n", n1, n2);
// Call the swap function
swap(&n1, &n2);
// Display values after swapping
printf("After swapping: n1 = %d, n2 = %d\n", n1, n2);
return 0;
}

Output
Input 1st number : 2
Input 2nd number : 4
Before swapping: n1 = 2, n2 = 4
After swapping: n1 = 4, n2 = 2
Result
Thus the C Program to swap two numbers using a function completed Successfully.

2.Write a program in C to check whether two given strings are an anagram.


Aim
To write a C Program to check whether two given strings are an anagram.
Algorithm

1. Input two strings: Read two strings from the user.


2. Check lengths: If the lengths of the two strings are not equal, return False because
anagrams must have the same number of characters.
3. Sort both strings: Sort both strings alphabetically.
4. Compare sorted strings: If the sorted strings are identical, then the strings are
anagrams. Otherwise, they are not anagrams.
5. Output result: Print whether the strings are anagrams or not.

Program

#include <stdio.h>
#include <string.h>
#include <ctype.h>
void checkAnagram(char str1[], char str2[]) {
// Step 1: Check if the lengths are the same
if (strlen(str1) != strlen(str2)) {
printf("%s and %s are NOT anagrams.\n", str1, str2);
return;
}
// Step 2: Sort the characters of both strings
// Using two loops to implement sorting (bubble sort)
for (int i = 0; i < strlen(str1) - 1; i++) {
for (int j = i + 1; j < strlen(str1); j++) {
// Sorting first string
if (str1[i] > str1[j]) {
char temp = str1[i];
str1[i] = str1[j];
str1[j] = temp;
}
// Sorting second string
if (str2[i] > str2[j]) {
char temp = str2[i];
str2[i] = str2[j];
str2[j] = temp;
}
}
}

// Step 3: Compare the sorted strings


if (strcmp(str1, str2) == 0) {
printf("%s and %s are Anagrams.\n", str1, str2);
} else {
printf("%s and %s are NOT anagrams.\n", str1, str2);
}
}
int main() {
char str1[100], str2[100];
// Step 4: Input strings from the user
printf("Input the first String: ");
fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0; // Remove the trailing newline character
printf("Input the second String: ");
fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0; // Remove the trailing newline character
// Step 5: Convert strings to lowercase to handle case insensitivity
for (int i = 0; str1[i]; i++) {
str1[i] = tolower(str1[i]);
}
for (int i = 0; str2[i]; i++) {
str2[i] = tolower(str2[i]);
}
// Step 6: Check for anagrams
checkAnagram(str1, str2);

return 0;
}
Output
Input the first String : spare
Input the second String : pears

spare and pears are Anagram.

Result

Thus the C program for checking whether two given strings are an anagram was
completed.

3.Write a program in C to print the Fibonacci Series using recursion.

Aim

To Write a C program to print the Fibonacci Series using recursion.

Algorithm
1. Input: Take the number of terms for the Fibonacci series from the user.
2. Base Case: Define the base cases for the Fibonacci series:
Fibonacci(0) = 0
Fibonacci(1) = 1
3. Recursive Case: For other values, the Fibonacci number is the sum of the two
preceding Fibonacci numbers:
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)
4. Call the Recursive Function: Call the recursive function to calculate and print the
Fibonacci series up to the desired number of terms.
5. Output: Print the Fibonacci numbers in the series.
Program
#include <stdio.h>
// Function to calculate Fibonacci number recursively
int fibonacci(int n) {
if (n <= 1) {
return 1; // Base case: return 1 for fibonacci(0) and fibonacci(1)
}
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}
int main() {
int terms;
// Input number of terms for the Fibonacci series
printf("Input number of terms for the Series (< 20): ");
scanf("%d", &terms);
// Ensure the number of terms is less than 20
if (terms >= 20 || terms <= 0) {
printf("Please enter a number less than 20 and greater than 0.\n");
return 1; // Exit if input is invalid
}
// Print Fibonacci series
printf("The Series are :\n");
for (int i = 0; i < terms; i++) {
printf("%d ", fibonacci(i)); // Call fibonacci() for each term
}
printf("\n");
return 0;
}

Output
Input number of terms for the Series (< 20) : 10
Input number of terms for the Series (< 20) : 10
The Series are :
1 1 2 3 5 8 13 21 34 55

Result
Thus the C Program to print the Fibonacci Series using recursion executed
successfully.
4.Write a program in C to find the GCD of two numbers using recursion.

Aim

To write a C program to find GCD of two numbers using recursion.

Algorithm
1. Input: Take two numbers, a and b, from the user.
2. Base Case: If b is 0, the GCD is a (because gcd(a, 0) = a).
3. Recursive Case: Otherwise, calculate the GCD of b and the remainder of a divided
by b (i.e., gcd(b, a % b)).
4. Call the Recursive Function: The function calls itself recursively until the base case
is reached.
5. Output: Print the GCD.

Program

#include <stdio.h>
// Function to calculate GCD using recursion
int gcd(int a, int b) {
if (b == 0) {
return a; // Base case: if b is 0, GCD is a
}
return gcd(b, a % b); // Recursive case: GCD(b, a % b)
}
int main() {
int num1, num2;
// Input the two numbers
printf("Input 1st number: ");
scanf("%d", &num1);
printf("Input 2nd number: ");
scanf("%d", &num2);
// Call the recursive gcd function
int result = gcd(num1, num2);
// Output the result
printf("The GCD of %d and %d is: %d\n", num1, num2, result);
return 0;
}

Output
Input 1st number: 10
Input 2nd number: 50

The GCD of 10 and 50 is: 10


Result
Thus the C Program find the GCD of two numbers using recursion executed
successfully.

5.Write a program in C to find the Factorial of a number using recursion.

Aim

To write a C program to find the Factorial of a number using recursion.

Algorithm

1. Input: Read an integer n from the user.


2. Base Case: If n is 0, return 1 (since 0! = 1).
3. Recursive Case: For any other number n > 0, return n * factorial(n - 1) which
recursively calculates the factorial.
4. Output: Print the factorial of the number.

Program

#include <stdio.h>
// Function to calculate the factorial of a number using recursion
int factorial(int n) {
if (n == 0) {
return 1; // Base case: 0! = 1
}
return n * factorial(n - 1); // Recursive case: n! = n * (n-1)!
}
int main() {
int num;
// Input the number
printf("Input a number: ");
scanf("%d", &num);
// Call the recursive factorial function
int result = factorial(num);
// Output the result
printf("The Factorial of %d is: %d\n", num, result);
return 0;
}

Output
Input a number : 5
The Factorial of 5 is : 120
Result

Thus the C Program find Factorial of a number using recursion executed successfully.

6.Write a program in C to add numbers using call by reference.

Aim

To write a C program to add numbers using call by reference.

Algorithm

1. Input: Read two numbers a and b from the user.


2. Call by Reference: Pass the addresses (pointers) of the variables a and b to a
function. This way, the function can modify the original values of the variables.
3. Add Numbers: Inside the function, add the values of a and b and store the result.
4. Output: Display the sum of the two numbers.

Program
#include <stdio.h>
// Function to add two numbers using call by reference
void addNumbers(int *a, int *b, int *sum) {
*sum = *a + *b; // Add the values of a and b, and store the result in sum
}
int main() {
int num1, num2, result;
// Input the two numbers
printf("Input the first number: ");
scanf("%d", &num1);
printf("Input the second number: ");
scanf("%d", &num2);
// Call the function with the addresses of num1, num2, and result
addNumbers(&num1, &num2, &result);
// Output the result
printf("The sum of %d and %d is %d\n", num1, num2, result);
return 0;
}

Output
Input the first number : 5
Input the second number : 6

The sum of 5 and 6 is 11

Result
Thus the C Program to add numbers using call by reference completed Successfully.
7. Write a program in C to swap elements using call by reference.

Aim

To write a C Program to swap elements using call by reference.

Algorithm

1. Input: Read three elements (a, b, c) from the user.


2. Display Initial Values: Display the values of the three elements before swapping.
3. Swap Logic: Use a temporary variable to swap the values of the three elements:
Swap a with c, and then b with a.
4. Output: Display the values of the three elements after swapping.

Program

#include <stdio.h>
// Function to swap three elements using call by reference
void swapElements(int *a, int *b, int *c) {
int temp;
// Swap a with c, and b with a
temp = *a;
*a = *c;
*c = *b;
*b = temp;
}
int main() {
int num1, num2, num3;
// Input values for the three elements
printf("Input the value of 1st element: ");
scanf("%d", &num1);
printf("Input the value of 2nd element: ");
scanf("%d", &num2);
printf("Input the value of 3rd element: ");
scanf("%d", &num3);
// Display values before swapping
printf("\nThe value before swapping are:\n");
printf("element 1 = %d\n", num1);
printf("element 2 = %d\n", num2);
printf("element 3 = %d\n", num3);
// Call the swap function with addresses of num1, num2, and num3
swapElements(&num1, &num2, &num3);
// Display values after swapping
printf("\nThe value after swapping are:\n");
printf("element 1 = %d\n", num1);
printf("element 2 = %d\n", num2);
printf("element 3 = %d\n", num3);
return 0;
}

Output
Input the value of 1st element : 5
Input the value of 2nd element : 6
Input the value of 3rd element : 7

The value before swapping are:


element 1 = 5
element 2 = 6
element 3 = 7

The value after swapping are:


element 1 = 7
element 2 = 5
element 3 = 6

Result

Thus the C Program to swap elements using call by reference completed Successfully.

You might also like