Find The Sum of Digits of a Given Number

Submitted by:Jhon Smith

Date added:30 April, 2014

Category:C++

C++ code example to find out the sum of given number

Tags: sum , sum digits

Code Snippet:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>

long int Sum_of_Dig(long int Number)
{
static long int sum=0;
if (Number==0)
{ return(sum);

}
else
{
sum=sum+Number%10+Sum_of_Dig(Number/10);
}
return(sum);
}

int main()
{
long int Num=0,Sum_dig=0;
printf("\n Enter Number : ");
scanf(\"%ld\",&Num);
Sum_dig=Sum_of_Dig(Num);
printf("\n Sum Of Digits :%ld ",Sum_dig);
return(0);

}
 
 

Comments