Sum of digits
Algorithm
Input An integer Output Sum of digits. complexity O(d).[no. of digit in the number] Sod(a) 1. [Initialize sum] sum := 0. 2. Repeat while(n>0) Set d=n%10 ; sum=sum+n; Set n=n/10 ; 3. return sum.
Algorithm Description
C Code
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,d,sum=0;
printf("\n enter any integer number :");
scanf("%d",&n);
while(n>0)
{
d=n%10;
sum+=d;
n=n/10;
}
printf("\ sum of digits= %d",sum);
return 0;
}