Question

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. 

07 Aug 2023
Answer :
Word Count : 1200

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

IGNOU NEWS
Assignment Submission Last Date Extended Till 30 June 2026 Click Here★★★IGNOU June 2026 TEE Date Sheet Released Click Here★★★
Top
📞
Call Support Instant phone assistance Write an interactive program using recursion for each of the following
🟢
WhatsApp Chat Fast live messaging
Email Us Business enquiries & support