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);
____ _________ ______ __________ ____ _________ ________ _________ _________.
_________ _______ __________ ______ ____ ______ ________ ______ ____ _________ _______ ______.
_____ _______ ____ _____ ______ __________ ____ ___ ___ ___.
_______ ________ __________ __________ _______ _____.
_______ _____ ________ __________ _________.
_________ _______ ________ ____ ________ ______ ________ _____ __________ _______.
_______ ___ ___ ____ _________ _________ ____ ____ ___ _____.
__________ _____ __________ ________ _________ ________ _______ ________ ________ _____.
____ _____ _________ _________ ______ ______.
_________ __________ _____ ___ ___ ________ ______ ____ ______.
_____ ________ ________ __________ ________.
______ ____ _______ _____ _______ _________.
____ ____ ________ __________ ___ ____ _________ ___.
__________ __________ ___ _______ _____ ___ _______ ______ __________ ___ ______ ____.
____ ___ _________ ___ ________ __________ ___ ______.
_____ _________ _____ ________ __________ ________ _____ ___ ________.
________ _______ ______ ______ _______ ______.
_________ ____ __________ ________ _________ ___.
_____ _____ ______ ___ _____ ________ ________ ___ _____ _________ ___.
_____ ___ _____ _______ _______ ________ ____.
________ ___ __________ __________ _______ ________ _________ ___ _____ ____ __________ _________.
_____ ___ ____ ___ ____ _________ ______.
___ ____ _____ ____ ___ _______ ___ ______ __________ ______ _________ _________.
______ _______ ___ ______ __________ _____.
__________ ______ ___ _______ ____ _____ ______ _____ __________.
_______ ________ _____ _________ _______.
_________ ___ _________ _______ _________ ________ _______.
_______ ___ _______ ______ _______ ____ _________ _______ _________.
_____ _______ ______ _________ _____ ___ ___ _______.
________ ___ _________ ____ ________ _________ ________.
_____ ______ ________ _____ _________ ________ _________.
___ ________ __________ ________ _______ _________ ________.
________ _________ ______ ______ _________ __________ __________ ________ ___ _______.
___ _________ _________ _________ ________ ___ ___ _________ ___ ____ ____ _________.
______ __________ __________ _______ _________ __________ __________ ______.
__________ ___ __________ ______ ______ __________ _______ _________ __________.
____ __________ _______ _____ ___ ____.
____ _____ ______ __________ _____ _________ ______ ________ ___.
_________ _____ ___ ______ ________ _____ ________ _____ _________ ______ _________ _______.
_____ ________ _____ ___ ______ _____ __________ ____ ______.
______ __________ ________ ____ _________ __________.
__________ _________ _______ _________ _______ ____ _________ ______ __________ ______ ________ ___.
___ _____ ______ _______ __________ _______ _____ _______ _______.
_______ __________ ___ ________ ______ ________ __________.
_____ _____ ________ _______ _______.
_______ ________ __________ ____ _________ _________ _______.
_____ ________ ___ _________ _______ __________ _____ _________ _____ _____ ___ __________.
_______ ___ ________ _____ ________ _________.
_____ ________ _____ _________ _______ _________ _____.
________ _________ _____ ______ _________ __________ _____ _____ _______ ____ ________ _________.
_____ _____ _______ ________ _____ ___ _________ __________.
_________ __________ ___ _____ ______ ________ ______ ____.
____ ______ ___ __________ __________ _____ _________ _________ _____ ___.
_______ ________ ________ __________ ___ ________ _______.
__________ __________ ___ ______ _______ __________ ________ ______ __________ __________.
__________ _______ ____ _____ __________.
__________ ____ __________ _________ ________.
_______ ______ ______ ________ ________ ____.
_____ _________ ___ ___ ____.
______ ___ ______ ___ ________ _______.
___ __________ ________ _____ ____ ______ ___ _____ __________ ___.
_________ ______ _________ _____ ________ _________ ___ ____ ________ _______ ________.
________ ____ ___ ______ _________ _________ ________ _________ _______.
________ __________ __________ ___ ___ __________ _______ _____.
_____ _______ ____ ____ ______.
_______ ______ _____ _________ _______ ________.
_______ ___ ____ _____ _______ ___ ________ _______ _________.
__________ ___ ___ _________ ________ __________.
______ ______ ________ ______ ____ ______ ____ __________ __________ ____.
_______ ____ _______ __________ _____ _______ _________.
______ ______ ______ ___ __________ ______ _____ __________ ________ __________.
_____ __________ ___ _________ _________.
______ _____ ____ ___ ______ __________ ___ ________.
_______ ___ ___ _______ _____.
_______ _______ ________ _______ __________ ________ _________ _____ _____ ___ ________.
________ ______ ___ __________ _________ ___ _______ _________ _____ _______ _______.
______ _________ ______ _____ ___ ___.
____ __________ _____ _________ _____ _______ _____ ________ _____ ________.
________ _____ ________ __________ __________ _______ _________ ____.
_________ __________ ________ ____ _________ ___ _______ _____ ___.
_____ _______ _________ ___ _________ _____ ________ ____ ____ _____ ____.
_____ ___ ________ _______ _______ _______ ___ ___ ____ _____.
_____ ___ _________ ________ ___ ______ _____.
_____ _________ _______ ________ ____ ___.
___ __________ __________ __________ _________ _________ ______.
______ ___ ____ ___ _________ ________ ____ ________ _______ ________ ________ _______.
____ ____ _______ ___ _________ ______ ___ _______ _____.
_________ _______ ______ ___ _________ _______ ________ ___ ______ ____ __________ _______.
___ _______ ______ ____ _______ _________ __________ ____ __________ ______ ______ ___.
_____ ___ _______ _________ _______.
______ ____ ________ _____ _____ _________ _________ ________ _________ ________ __________.
________ ________ __________ _______ _____ ___ _______.
_____ ________ __________ _______ ________ ____ _______ _______ _____ _____.
______ _________ _______ _________ __________ __________ _________ ________ _____ ________ _______ _________.
____ ________ ___ _____ __________ ________ ________.
________ ___ _____ _________ ____ ____ __________ ____.
__________ ____ _______ ________ _________.
_______ ___ ___ _____ ________ __________ _________ _______ ________ ___ ______ _______.
___ __________ _________ ____ ______ ____ _______ _____ _________ _____.
___ ____ _______ ______ ________ _____ ___ ______ _______ ________ _____ __________.
__________ _____ ________ ___ _____ ___ __________ _____ ________ _________.
____ _________ ________ ___ ________ ______ ______ ________ _________ __________.
_________ ____ ____ _____ ______ __________ ____ ______ _____ _____ ___.
_____ ____ _____ _________ ________ ______ ____.
______ _________ _______ ______ _____ _________ __________ _____.
______ ___ ________ _______ ________ _____ ____ _________ ___ _____ __________.
_____ ______ ________ ______ _______ ____ _________ _______.
_____ ____ ________ _____ ____ _____ ____ __________ _______ ____ ___ ______.
_________ _______ ___ __________ __________ ________.
___ __________ ______ ______ _____ ______ __________ ____ _____ ____.
________ ____ _________ __________ _______ __________ ___ ________ ____ ________ _________.
____ _______ __________ _________ ____ _______.
_____ ______ _____ _____ ________ __________ ______ __________ _________ _________ ________.
______.
Get Full Answer on WhatsApp