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

12 comments:

Unknown said...

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

CProgrammer said...

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;
}

Anonymous said...

s should be initialized with 0

dgdinkar said...
This comment has been removed by the author.
dgdinkar said...

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

Anonymous said...

initialize with zero

Anonymous said...

ya u r right...

Anonymous said...

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

Anonymous said...

what's the meaning of if (n) ??

Anonymous said...

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

Gokulnath N said...

int sum = 0;
if (num < 10)
return num;
else
{
sum = num % 10;
num = num / 10;
num = num + sum;
return Add_Digits(num);
}

Unknown said...

its global variable
its initialize with 0 by default