EXP 10 PPS Lab
EXP 10 PPS Lab
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.
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;
}
}
}
return 0;
}
Output
Input the first String : spare
Input the second String : pears
Result
Thus the C program for checking whether two given strings are an anagram was
completed.
Aim
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
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
Aim
Algorithm
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.
Aim
Algorithm
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
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
Algorithm
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
Result
Thus the C Program to swap elements using call by reference completed Successfully.