Q:

Write a C Program to display reverse number and find sum of digits by recursion

0

Write a C Program to display reverse number and find sum of its digits using recursion. C program to find sum of digits and reverse of a number. How to find sum of digits of a number and reverse of a number in C programming. Logic to find sum of digits and reverse of a given number in C program.

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.

Reverse a number : :


This program reverse the number entered by the user and then prints the reversed number on the screen. For example if user enter 123 as input then 321 is printed as output.

 
 

In our program we use modulus(%) operator to obtain the digits of a number. To invert number look at it and write it from opposite direction or the output of code is a number obtained by writing original number from right to left.

To reverse or invert large numbers use long data type or long long data type if your compiler supports it, if you still have large numbers then use strings or other data structure.


Sum of digits : :


This C Program computes the sum of digits in a given integer. This program m accepts integer. Then adds all the digits of a given integer, that becomes the sum of digits of integer.

 

Below is the source code for C Program to display reverse number and find sum of its digits by recursion which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/* Program to display reverse number and find sum of its digits*/

#include<stdio.h>
void display(long int n);
void Rdisplay(long int n);
int sumdigits( long int n);

int main( )
{
        long int num;
        printf("Enter number : ");
        scanf("%ld", &num);
        printf("\nSum of digits :: ");
        printf("%d\n",sumdigits(num));
        printf("\n");
        printf("Original number :: ");
        display(num);
        printf("\n");
        printf("\nReverse number :: ");
        Rdisplay(num);
        printf("\n");

        return 0;

}/*End of main()*/

/*Finds the sum of digits of an integer*/
int sumdigits(long int n)
{
        if( n/10 == 0 ) /* if n is a single digit number*/
                return n;
        return n%10 + sumdigits(n/10);
}/*End of sumdigits()*/

/*Displays the digits of an integer*/
void display(long int n)
{
        if( n/10==0 )
        {
                printf("%d",n);
                return;
        }
        display(n/10);
        printf("%d",n%10);
}/*End of display()*/

/*Displays the digits of an integer in reverse order*/
void Rdisplay(long int n)
{
        if(n/10==0)
        {
                printf("%d",n);
                return;
        }
        printf("%d",n%10);
        Rdisplay(n/10);
}/*End of Rdisplay()*/

OUTPUT  : :


//------------------OUTPUT ::----------------------------


//------------------FIRST RUN ----------------------------

Enter number : 123456

Sum of digits :: 21

Original number :: 123456

Reverse number :: 654321



//-------------------SECOND RUN ::-------------------------


Enter number : 4352747

Sum of digits :: 32

Original number :: 4352747

Reverse number :: 7472534

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

total answers (1)

C Recursion 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 raise float to power integer ... >>
<< C Program to convert decimal number to Binary, Oct...