Q Here are the recursion programming examples from Program 9.13 to 9.20, complete with detailed inline comments explaining the base cases, general recursive steps, and the behavior of static variables.
PROGRAM 9.13: Add All the Digits of a Number
This function extracts the last digit of the number using the modulo operator (%) and recursively adds it to a static running sum.
#include<stdio.h>
int add_digit (int n)
{
// 'static' variables retain their values across multiple recursive calls.
// 'r' holds the extracted digit, 's' holds the cumulative sum.
static int r, s;
// BASE CASE: If the number is reduced to 0, stop and return 0.
if (n == 0)
return 0;
else
{
r = n % 10; // Extract the last digit
s = s + r; // Add the digit to the running sum
add_digit(n / 10); // GENERAL CASE: Pass the remaining digits recursively
}
return s; // Returns the final sum after the base case is hit
}
void main()
{
int n, res;
printf("Enter a number ");
scanf("%d", &n);
res = add_digit(n);
printf("Result=%d ", res);
}PROGRAM 9.14: Check if a Number is a Palindrome
This program uses a recursive function to mathematically reverse a number and then checks if the reversed number equals the original input.
#include<stdio.h>
// This function effectively reverses the digits of the number
int add_digit (int n)
{
static int r, s;
// BASE CASE: Stop recursion when no digits are left.
if (n == 0)
return 0;
else
{
r = n % 10; // Extract last digit
s = s * 10 + r; // Shift the previous digits left and add the new digit
add_digit(n / 10); // GENERAL CASE: Pass the remaining digits
}
return s;
}
void main()
{
int n, res;
printf("Enter a number ");
scanf("%d", &n);
res = add_digit(n);
// Compare the reversed number (res) with the original number (n)
if (res == n)
printf("pallindrome");
else
printf("Not Pallindrome");
}PROGRAM 9.15: Reverse a Number
This logic is identical to the function used in Program 9.14, but it specifically outputs the reversed number rather than using it for a palindrome check.
#include<stdio.h>
int rev_digit (int n)
{
// 'static' prevents 'r' and 's' from resetting to 0 on each call
static int r, s;
if (n == 0) // BASE CASE
return 0;
else
{
r = n % 10;
s = s * 10 + r;
rev_digit(n / 10); // GENERAL CASE
}
return s;
}
void main()
{
int n, res;
printf("Enter a number ");
scanf("%d", &n);
res = rev_digit(n);
printf("Reverse number=%d", res);
}PROGRAM 9.16: Add the Numbers from 1 to n
This recursively adds to the sum of all numbers before it until it reaches 0.
#include<stdio.h>
int add (int n)
{
// BASE CASE: Stop recursion when n reaches 0.
if (n == 0)
return 0;
// GENERAL CASE: Add current n to the sum of (n-1).
else
return n + add(n - 1);
}
void main()
{
int n, res;
printf("Enter a number ");
scanf("%d", &n);
res = add(n);
printf("Result=%d", res);
}PROGRAM 9.17: Print the Numbers from 1 to n
This function uses a static counter to count upwards and print values until the limit n is reached.
#include<stdio.h>
void print (int n)
{
// 'static i' starts at 1 and increments globally across recursive calls
static int i = 1;
// BASE CASE: Stop printing when the counter exceeds the limit 'n'
if(i > n)
return;
else
{
printf("%d ", i++); // Print the current value of i, then increment it
print(n); // GENERAL CASE: Call again with the same limit 'n'
}
}
void main()
{
int n;
printf("Enter a number ");
scanf("%d", &n);
print(n);
}PROGRAM 9.18: Print the Numbers from n to 1
This function prints the current number first, then calls itself with a decremented value. (Note: The source textbook contains a typo here where it uses print() instead of rev_print(). I have left the source code exactly as it appears in the text, but added a comment so you understand the error).
#include<stdio.h>
void rev_print (int n)
{
// BASE CASE: Stop when n hits 0
if (n == 0)
return;
else
{
printf("%d ", n); // Print the current n
print(n - 1); // EXAM HACK: The textbook wrote 'print(n-1)', but logically this should be 'rev_print(n-1)'
}
}
void main()
{
int n;
printf("Enter a number ");
scanf("%d", &n);
print(n); // EXAM HACK: The textbook wrote 'print(n)', but it should be calling 'rev_print(n)'
}PROGRAM 9.19: Check if a Number is a Prime Number
This function recursively checks how many times n can be perfectly divided by incrementing a divisor i from 1 to n. A prime number will have exactly 2 divisors.
#include<stdio.h>
int prime (int n)
{
// 'static i' is the divisor being tested, 'static c' counts successful divisions
static int i = 1, c = 0;
// BASE CASE: Stop checking when divisor 'i' exceeds the number 'n'
if(i > n)
return 0;
else
{
if(n % i == 0) // If perfectly divisible, increment the counter 'c'
c = c + 1;
i = i + 1; // Move to the next divisor
prime(n); // GENERAL CASE: Recursive call
}
return c; // Return the total count of divisors
}
void main()
{
int n, count;
printf("Enter a number ");
scanf("%d", &n);
count = prime(n);
// A prime number is strictly divisible by exactly two numbers (1 and itself)
if (count == 2)
printf("Prime Number");
else
printf("Not Prime Number");
}PROGRAM 9.20: Generate a Fibonacci Series
This function calculates the next term in a Fibonacci sequence recursively. (Note: Similar to 9.18, the source textbook contains a typo where it recursively calls print(a,b,n) instead of fibonacci(a,b,n)).
#include<stdio.h>
void fibonacci (int a, int b, int n)
{
// 'static c' stores the newly generated Fibonacci term
static int c;
// BASE CASE: Stop printing when the calculated term exceeds 'n'
if (c > n)
return;
else
{
c = a + b; // Calculate the next term
printf("%d ", c); // Print the term
// Shift values: 'a' becomes the old 'b', and 'b' becomes the new term 'c'
a = b;
b = c;
print(a, b, n); // EXAM HACK: The textbook wrote 'print(a,b,n)', but logically it must be 'fibonacci(a,b,n)'
}
}
void main()
{
int a = 0, b = 1;
printf("%d ", a); // Print the 0th term
printf("%d ", b); // Print the 1st term
fibonacci(a, b, 10); // Generate the rest of the series up to the maximum value 10
}