Testing.

Tuesday, 3 January 2017

C programming language: C program to calculate/ find sum of super digits using recursion.

For Example:- 
                 super digit of 9875 will be calculated as:
super-digit(9875) = super-digit(9+8+7+5) 
                  = super-digit(29) 
                  = super-digit(2+9)
                  = super-digit(11)
                  = super-digit(1+1)
                  = super-digit(2)
                  = 2.
-----------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
int recSum(int n);
void main()
{
int n,ans;
printf("\n Enter number: ");
scanf("\n%d",&n);
ans=recSum(n);
printf("\n Sum:=%d",ans);
getch();
}

int recSum(int n1)
{
int rem,sum=0;
if(n1==0)
return 0;
else
{
rem=n1%10;
sum=rem+recSum(n1/10);
}

if(sum>9)
{
n1=sum;
sum=recSum(n1);
}
return sum;
}


3 comments: