You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
487 B
C
29 lines
487 B
C
5 years ago
|
/* 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
|
||
|
*/
|