Slip
no : 24_2 write a C prog to calculate sum of digit till it reduces to single
digit using recursion
#include<stdio.h>
#include<conio.h>
int getSum(int n)
{
static int sum=0;int no;
if(n>0)
{
sum = sum
+ n%10; getSum(n/10);
}
else if(sum>=10) { no=sum;
sum=0;
getSum(no);
}
return sum;
}
main()
{
int n,sum;
printf("Enter
the value of n: "); scanf("%d",&n);
sum = getSum(n);
printf("Sum of n numbers:
%d",sum);
getch();
}
No comments:
Post a Comment