Q:

Write a C Program to convert Number to String using Recursion

0

Write a C Program to convert Number to String using Recursion. Here’s simple Program to convert Number to String using Recursion in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Recursion : :


  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Below is the source code for C Program to convert Number to String using Recursion which is successfully compiled and run on Windows System to produce desired output as shown below :

 
 

SOURCE CODE : :

/* C Program to convert number to string */


#include<stdio.h>
void f(long int n, char str[]);
int main()
{
        long int num;
        printf("Enter any number to convert to String :: ");
        scanf("%ld",&num);
        char str[30];
        f(num, str);
        printf("\nAfter Converting Number to String :: ");
        puts(str);

    printf("\nEnter another number to convert to String :: ");
        scanf("%ld",&num);
        f(num, str);
        printf("\nAfter Converting 2nd Number to String :: ");
        puts(str);
}

void f(long int n, char s[])
{
        static int i=0;
        if(n==0)
        {
                i=0; /*If value of i is not made zero here, then fn will not work
                        correctly if called more than once in the main() */

       return;
        }
        f(n/10, s);
        s[i++] = n%10  + '0';
        s[i]='\0';
}

OUTPUT  : :


***************** OUTPUT **********************


Enter any number to convert to String :: 1234

After Converting Number to String :: 1234

Enter another number to convert to String :: 6789

After Converting 2nd Number to String :: 6789

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C String Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to convert string of numbers to ... >>
<< Write a C Program to Replace occurence of characte...