Write an interactive program using recursion for each of the following:
(a) To count the digits of a given number.
(b) To reverse a string
(c) To find the least-common-multiple of two numbers.
Here's an interactive program using recursion for each of the tasks you mentioned:
(a) To count the digits of a given number:
#include <stdio.h>
int countDigits(int number)
{
if (number == 0)
return 0;
else
return 1 + countDigits(number / 10);
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
int digitCount = countDigits(number);
printf("Number of digits: %d\n", digitCount);
return 0;
}
Explanation: The countDigits function takes an integer number as a parameter and uses recursion to count the number of digits in the number. The base case is when the number becomes 0, in which case the function returns 0. Otherwise, it divides the number by 10 to remove the rightmost digit and recursively calls itself with the updated number. It then adds 1 to the result of the recursive call and returns the sum. In the main function, the user is prompted to enter a number, and the countDigits function is called with that number as the argument. The number of digits is then displayed on the console.
(b) To reverse a string:
#include <stdio.h>
void reverseString(char *str)
{
if (*str)
{
reverseString(str + 1);
printf("%c", *str);
}
}
int main()
{
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("Reversed string: ");
reverseString(str);
printf("\n");
return 0;
}
Explanation: The reverseString function takes a character pointer str as a parameter and uses recursion to reverse the string. It checks if the current character is not a null character (*str), which indicates the end of the string. If it's not null, the function makes a recursive call by incrementing the character pointer str to the next character. This effectively moves the pointer towards the end of the string. After the recursive call, the function prints the current character (*str), which results in printing the characters in reverse order. In the main function, the user is prompted to enter a string, and the reverseString function is called with the string as the argument. The reversed string is then displayed on the console.
(c) To find the least common multiple (LCM) of two numbers:
#include <stdio.h>
int findLCM(int num1, int num2, int lcm)
{
if (lcm % num1 == 0 && lcm % num2 == 0)
return lcm;
else
return findLCM(num1, num2, lcm + 1);
}
int main()
{
int number1, number2;
printf("Enter two numbers: ");
scanf("%d %d", &number1, &number2);
________ ____ __________ ______ _______ __________ _________ ____ ________ ________ ___.
__________ _________ ____ __________ ______ __________ __________ _____ ___ __________ _____.
____ ____ _________ ________ ______ ___ ______ _________ _________.
_______ ______ ____ _______ _____ _____ _______ ___ __________ _______ ___ __________.
___ _____ ________ __________ ________ __________ ________ ______ _______ _____ __________.
_____ ____ _____ ______ ________ ___ _________ _____ ______ ______.
__________ _______ ____ ____ ________ _______ ________ __________ _________ __________ _____.
___ ____ ______ ___ ____ __________ _____.
_______ __________ ______ _________ __________ ___ _____ ______.
____ ____ ________ ___ __________.
________ __________ ________ __________ ____ ____ ______ _______.
_______ _______ ________ _______ __________ _____.
________ ________ ________ ________ ___ ___ _______ ___.
_________ _______ _______ ________ _____ ____ ________ __________ ________ __________ ______ ___.
_______ ________ __________ _____ ______.
_______ _________ _____ _________ ________ ________ ________ ______ __________ _____ __________.
____ __________ _________ __________ _____ ________.
_______ _____ ___ _____ _____ __________ _________ ___.
___ _________ ____ _______ _____ _______ _________ ________ ___ _____.
____ _________ _________ ___ __________ _____.
_________ _____ _________ ______ _____ __________ ______ ______ _________ _________.
___ ________ ______ _______ ______ _____ ______ __________ ___ ____ __________.
_______ _______ _____ ____ _______ ___ _____ ______ ________ _________ ______.
_________ ________ __________ _______ ________ __________ _____ ______.
___ ________ __________ ______ _____ ___ _____.
____ ___ ______ __________ ________.
______ ___ _________ ______ ____ ________.
________ _____ ___ ____ __________ _____ ________ _________ _______ _________.
______ ________ _______ ___ _______ _________ ________ __________ _____ _________.
_______ __________ _________ ______ __________ ______ __________ _____ __________.
____ _______ ________ ____ _________ ________ ________.
__________ ________ __________ ________ ____ _____ __________ _____ ________ _________ ________ _______.
_________ _________ _________ ___ ______ __________.
______ __________ ___ ____ ________ ______ _________ ____ _____ ____.
__________ _________ __________ _________ __________ ________ __________.
______ _________ _______ _______ ____ ____.
__________ __________ ____ ____ ____ _________.
________ __________ _____ _____ _________ ___ ___ _____ __________ ______ ____.
_______ ____ ________ ___ ___ _______ _______ _______.
________ ___ __________ ______ ________.
__________ ______ __________ ________ __________ ________ ____ ___.
______ _____ __________ ____ _______ ______ __________ ________.
___ ____ ________ _________ _________ ______ __________ ________ ______ ________.
_________ _____ ______ ____ ______.
_________ _________ _______ _________ ______.
____ _______ ___ _____ ____.
___ _______ _____ _________ ____ _________ ____ ______ ___ ________.
_______ _______ _________ ____ _________ ________.
_________ ____ ____ _____ _______.
_________ _______ __________ ________ ________ _____ _______ ________ ____ _____.
________ _______ ______ ________ ______ ______ _____.
________ ____ ________ ___ _______ _____ ______ ______ ___ _____ _______ ________.
_____ _______ ____ ____ __________.
________ _________ _______ __________ ______ _____ ___ _______ ___ _____ _________ _______.
______ __________ __________ _____ ________ __________ _____.
_____ ______ __________ ______ _________ ________ ________ ___ ________ ______.
__________ ____ ______ _________ _____ ________ ______ _______ ___ ________.
______ _____ ______ __________ _______ ___ _______.
___ ____ ___ ___ _________ _______ _________ ___.
_________ ___ __________ __________ ___ ____ ____ _______ ___ __________ ___ ______.
______ ______ __________ __________ _______ ______.
_______ ___ _______ ____ _____ _____ _________ ____ ________ __________ __________.
___ _________ _______ ____ ____.
_______ _________ ________ ________ __________ ___ _______ ______ _________ ____ __________.
________ ________ _________ ____ ____ __________ __________ _______ ______ _____ _______.
_____ __________ ______ _________ _______ ___ _________ ___ _______ ___ ___.
_______ ___ _____ ______ ______ _________.
_________ ____ ___ _______ ______ _______ ______.
___ ___ _____ ____ ______ ________.
___ __________ ___ ________ _____ _________ ______ ____ ____ _________.
________ ________ ________ ______ ______ ______ ____.
_____ __________ __________ __________ _________ ________ _____ ____ ______ _____ ________ __________.
___ __________ _____ ________ __________ ___ _______.
_________ _______ _______ ________ _________ _____.
____ _______ __________ _____ ____ ________ _________ ______.
___ _____ _____ ___ ____ _____ ____ __________ __________ __________ __________ __________.
_______ _____ ________ __________ _____ _________.
__________ _________ __________ _____ ____ __________ ________.
_____ _______ __________ ______ ___ ____ _______ _________ ___ __________.
___ ________ _____ _______ _____ ___ __________ __________.
______ _________ __________ _______ _____.
___ __________ _____ __________ _______.
_____ _______ __________ _________ _______.
_________ ____ _____ ___ _________ _______ _________ ______ ___ ______ ____.
___ ________ ____ _________ ___ ___ ______ ______.
_____ ______ ___ ______ ___.
__________ _____.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required