FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM






Sum of digits in c using recursion


#include<stdio.h>
int main(){
  int num,x;
  clrscr();
  printf("\nEnter a number: ");
  scanf("%d",&num);
  x=findsum(num);
  printf("Sum of the digits of %d is: %d",num,x);
  return 0;
}

int r,s;
int findsum(int n){
if(n){
         r=n%10;
         s=s+r;
         findsum(n/10);
     }
     else
       return s;
}





Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

10 comments:

  1. write a program to add the following
    ¼+2/4+3/4+5/3+6/3+………

    ReplyDelete
  2. You should declare function int findsum(int) and line 18 (else) shouldn't be included, because the output is incorrect...here is the right source code:

    #include
    int findsum(int);
    int main(){
    int num,x;

    printf("\nEnter a number: ");
    scanf("%d",&num);
    x=findsum(num);
    printf("Sum of the digits of %d is: %d",num, x);
    return 0;
    }

    int r, s;
    int findsum(int n){

    if(n){
    r=n%10;
    s=s+r;
    findsum(n/10);
    }
    return s;
    }

    ReplyDelete
  3. s should be initialized with 0

    ReplyDelete
    Replies
    1. No need as it is global so already initialized with 0.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. initialize with zero

    ReplyDelete
    Replies
    1. ya u r right...

      Delete
  6. The variable s treats as a external variable.the default value of s is automatically initialized to 0.

    ReplyDelete
    Replies
    1. how will you find the sum of the digits of the resulting sum of digits if it is more than 10?

      Delete
  7. what's the meaning of if (n) ??

    ReplyDelete