0% found this document useful (0 votes)
3 views8 pages

Recursion Programs

Uploaded by

meganeshprasath
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)
3 views8 pages

Recursion Programs

Uploaded by

meganeshprasath
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

RECURSION PROGRAMS

1. C Program to Calculate Factorial Using Recursion

#include <stdio.h>
// Function to calculate factorial using recursion
int factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: factorial of 0 or 1 is 1
} else {
return n * factorial(n - 1); // Recursive case
}
}

int main() {
int num;

// Get user input


printf("Enter a number: ");
scanf("%d", &num);

// Check if the number is negative


if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate and print the factorial
printf("Factorial of %d is: %d\n", num, factorial(num));
}

return 0;
}

2. C Program to Calculate Fibonacci Number Using Recursion

#include <stdio.h>
// Function to calculate the Fibonacci number using recursion
int fibonacci(int n) {
if (n <= 1) {
return n; // Base case: Fibonacci(0) = 0, Fibonacci(1) = 1
} else {
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}
}

int main() {
int num;

// Get user input


printf("Enter a number: ");
scanf("%d", &num);
// Check if the number is non-negative
if (num < 0) {
printf("Fibonacci is not defined for negative numbers.\n");
} else {
// Calculate and print the Fibonacci number
printf("Fibonacci of %d is: %d\n", num, fibonacci(num));
}

return 0;
}

ARRAYS
C Program to Find the Smallest Element in an Array
#include <stdio.h>

int main() {
int n, i;

// Get the number of elements in the array


printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

// Get the array elements from the user


printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Assume the first element is the smallest


int smallest = arr[0];

// Loop through the array to find the smallest element


for (i = 1; i < n; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}

// Print the smallest element


printf("The smallest element in the array is: %d\n", smallest);

return 0;
}
C Program to Sort Elements in an Array
#include <stdio.h>

int main()
{
int arr[] = {50, 20, 80, 70, 10};
int temp = 0;

int length = sizeof(arr)/sizeof(arr[0]);

printf("Elements of original array: \n");


for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}

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


for (int j = i+1; j < length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

printf("\n");

printf("Elements of array sorted in ascending order: \n");


for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Write a C program to compute XY using function and without using math library.

#include <stdio.h>

// Function to calculate X^Y


int power(int x, int y) {
int result = 1; // Initialize result to 1 (since any number to the power of 0 is 1)

// Loop to multiply x by itself y times


for (int i = 1; i <= y; i++) {
result *= x; // Multiply result by x in each iteration
}

return result;
}
int main() {
int x, y;

// Input the base and exponent


printf("Enter the base (X): ");
scanf("%d", &x);

printf("Enter the exponent (Y): ");


scanf("%d", &y);

// Call the power function and print the result


int result = power(x, y);
printf("%d raised to the power of %d is %d\n", x, y, result);

return 0;
}

C Program to Find the Grade of a Student using functions


#include <stdio.h>

// Function to determine the grade based on marks


char getGrade(int marks) {
if (marks >= 90) {
return 'A'; // Grade A for marks 90 and above
} else if (marks >= 80) {
return 'B'; // Grade B for marks between 80 and 89
} else if (marks >= 70) {
return 'C'; // Grade C for marks between 70 and 79
} else if (marks >= 60) {
return 'D'; // Grade D for marks between 60 and 69
} else {
return 'F'; // Grade F for marks below 60
}
}

int main() {
int marks;
char grade;

// Input the student's marks


printf("Enter the marks of the student: ");
scanf("%d", &marks);

// Call the getGrade function to determine the grade


grade = getGrade(marks);

// Output the grade


printf("The grade of the student is: %c\n", grade);
return 0;
}

MATRIX
C Program to Find the Sum of Diagonal Elements of a 3x3 Matrix:

#include <stdio.h>

int main() {
int matrix[3][3];
int sumMainDiagonal = 0, sumSecondaryDiagonal = 0;

// Input the elements of the 3x3 matrix


printf("Enter the elements of the 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &matrix[i][j]);
}
}

// Calculate the sum of diagonal elements


for (int i = 0; i < 3; i++) {
sumMainDiagonal += matrix[i][i]; // Main diagonal element (i, i)
sumSecondaryDiagonal += matrix[i][3 - i - 1]; // Secondary diagonal element (i, 3-i-1)
}

// Output the results


printf("Sum of main diagonal elements: %d\n", sumMainDiagonal);
printf("Sum of secondary diagonal elements: %d\n", sumSecondaryDiagonal);

return 0;
}

STRING PROGRAMS( WITHOUT USING BUILT IN FUNCTIONS)

1. String Length Calculation (Without Built-in Functions)

#include <stdio.h>

void stringLength(char str[]) {


int length = 0;
while (str[length] != '\0') {
length++;
}
printf("Length of the string: %d\n", length);
}

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str); // Using scanf instead of fgets

stringLength(str);
return 0;
}
OUTPUT
Enter a string: Hello
Length of the string: 5

2. String Copy (Without Built-in Functions)


#include <stdio.h>

void stringCopy(char source[], char destination[]) {


int i = 0;
while (source[i] != '\0') {
destination[i] = source[i];
i++;
}
destination[i] = '\0'; // Null-terminate the destination string
printf("Copied string: %s\n", destination);
}

int main() {
char source[100], destination[100];

printf("Enter a string to copy: ");


scanf("%s", source); // Using scanf instead of fgets

stringCopy(source, destination);
return 0;
}
Sample Output:
Enter a string to copy: Hello
Copied string: Hello

3. String Concatenation (Without Built-in Functions)


#include <stdio.h>

void stringConcatenate(char str1[], char str2[]) {


int i = 0, j = 0;

// Find the end of the first string


while (str1[i] != '\0') {
i++;
}

// Append the second string to the first string


while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}

str1[i] = '\0'; // Null-terminate the concatenated string


printf("Concatenated string: %s\n", str1);
}

int main() {
char str1[100], str2[100];

printf("Enter the first string: ");


scanf("%s", str1); // Using scanf instead of fgets
printf("Enter the second string: ");
scanf("%s", str2); // Using scanf instead of fgets

stringConcatenate(str1, str2);
return 0;
}
Sample Output:
Enter the first string: Hello
Enter the second string: World
Concatenated string: HelloWorld

4. String Comparison (Without Built-in Functions)


#include <stdio.h>

int stringCompare(char str1[], char str2[]) {


int i = 0;
while (str1[i] != '\0' && str2[i] != '\0') {
if (str1[i] != str2[i]) {
return 0; // Strings are not equal
}
i++;
}
return (str1[i] == '\0' && str2[i] == '\0'); // Check if both strings are of the same length
}

int main() {
char str1[100], str2[100];

printf("Enter the first string: ");


scanf("%s", str1); // Using scanf instead of fgets
printf("Enter the second string: ");
scanf("%s", str2); // Using scanf instead of fgets

if (stringCompare(str1, str2)) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}

return 0;
}
Sample Output:
Enter the first string: Hello
Enter the second string: Hello
The strings are equal.

5. String Reversal (Without Built-in Functions)


#include <stdio.h>

void stringReverse(char str[]) {


int start = 0, end = 0;
char temp;

// Find the length of the string


while (str[end] != '\0') {
end++;
}
end--; // Move back to the last character of the string

// Reverse the string


while (start < end) {
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
printf("Reversed string: %s\n", str);
}

int main() {
char str[100];

printf("Enter a string to reverse: ");


scanf("%s", str); // Using scanf instead of fgets

stringReverse(str);
return 0;
}
Sample Output:
Enter a string to reverse: Hello
Reversed string: olleH

You might also like