programming-examples/c/Functions/C Program for Sum of digits use recursive function to calculate sum of digits of an integer..c

29 lines
487 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
/* Sum of digits use recursive function to calculate sum of digits of an integer*/
#include <stdio.h>
#include <conio.h>
int sum(int n)
{
if(n==0)
return 0 ;
else
return( n%10 + sum(n/10) ) ;
}
void main()
{
int n ;
clrscr() ;
printf("Enter the number: ") ;
scanf("%d", &n) ;
printf("Sum of digits of %d is %d", n, sum(n) ) ;
getch() ;
}
/*
Output:
Enter the number: 246
Sum of digits of 246 is 12
*/