Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Recursive Function

The document provides an overview of recursive functions in C programming, including examples for calculating factorials, Fibonacci numbers, reversing strings, printing numbers, summing digits, and checking for palindromes. Each example includes a C code snippet demonstrating the recursive approach. The document highlights the base cases and recursive calls essential for implementing these functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Recursive Function

The document provides an overview of recursive functions in C programming, including examples for calculating factorials, Fibonacci numbers, reversing strings, printing numbers, summing digits, and checking for palindromes. Each example includes a C code snippet demonstrating the recursive approach. The document highlights the base cases and recursive calls essential for implementing these functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Recursive Functions

The recursion process in C refers to the process in which the program repeats a certain section of
code in a similar way. Thus, in programming languages, when the program allows the user to call
any function inside the very same function, it is referred to as a recursive call in that function.

EX1

C program to calculate the factorial of 5.


#include <stdio.h>

// Function to calculate factorial


int factorial(int n) {
if (n == 0) {
return 1; // Base case: 0! = 1
}
return n * factorial(n - 1); // Recursive call
}

int main() {
int number = 5;
int result = factorial(number);
printf("The factorial of %d is %d.\n", number, result);
return 0;
}

EX2

C program that takes an input from the user and prints the factorial of the entered value.

#include <stdio.h>

// Function to calculate factorial


int factorial(int n) {
if (n == 0) {
return 1; // Base case: 0! = 1
}
return n * factorial(n - 1); // Recursive call
}

int main() {
int number;

// Prompt the user for input


printf("Enter a number to calculate its factorial: ");
scanf("%d", &number);

// Calculate the factorial


int result = factorial(number);

// Print the result


printf("The factorial of %d is %d.\n", number, result);

return 0;
}
EX3

Write a recursive function to calculate the nth Fibonacci number.

#include <stdio.h>

// Function to calculate nth Fibonacci number


int fibonacci(int n) {
if (n <= 1) {
return n; // Base cases: fib(0) = 0, fib(1) = 1
}
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive calls
}

int main() {
int n;
printf("Enter a number to calculate its Fibonacci: ");
scanf("%d", &n);
int result = fibonacci(n);
printf("The %dth Fibonacci number is %d.\n", n, result);
return 0;
}

EX4

Write a recursive function to reverse a given string.

#include <stdio.h>
#include <string.h>

// Function to reverse a string


void reverse(char *str, int start, int end) {
if (start >= end) {
return; // Base case
}
// Swap characters
char temp = str[start];
str[start] = str[end];
str[end] = temp;
// Recursive call
reverse(str, start + 1, end - 1);
}
int main() {
char str[100];
printf("Enter a string to reverse: ");
scanf("%s", str);
int length = strlen(str);
reverse(str, 0, length - 1);
printf("The reversed string is %s.\n", str);
return 0;
}

EX5

Write a recursive function to print numbers from 1 to N.

#include <stdio.h>

// Function to print numbers from 1 to N


void printNumbers(int n) {
if (n < 1) {
return; // Base case
}
printNumbers(n - 1); // Recursive call
printf("%d ", n); // Print the number
}

int main() {
int n;
printf("Enter a number to print numbers from 1 to N: ");
scanf("%d", &n);
printNumbers(n);
printf("\n");
return 0;
}

EX6

Write a recursive function to calculate the sum of digits of a given number.

#include <stdio.h>

// Function to calculate sum of digits


int sumOfDigits(int n) {
if (n == 0) {
return 0; // Base case
}
return (n % 10) + sumOfDigits(n / 10); // Recursive call
}

int main() {
int number;
printf("Enter a number to calculate sum of its digits: ");
scanf("%d", &number);
int result = sumOfDigits(number);
printf("The sum of digits of %d is %d.\n", number, result);
return 0;
}

EX7

Write a recursive function to check if a given number, is a palindrome.

#include <stdio.h>

// Helper function to reverse the number


int reverseNumber(int n, int temp) {
if (n == 0) {
return temp; // Base case
}
temp = (temp * 10) + (n % 10);
return reverseNumber(n / 10, temp); // Recursive call
}

// Function to check if the number is a palindrome


int isPalindrome(int n) {
int temp = reverseNumber(n, 0);
return (n == temp); // Check if reversed number is the same
}

int main() {
int number;
printf("Enter a number to check if it is a palindrome: ");
scanf("%d", &number);
if (isPalindrome(number)) {
printf("%d is a palindrome.\n", number);
} else {
printf("%d is not a palindrome.\n", number);
}
return 0;
}

You might also like