C++ Program to Find Sum of Digits of a Number

C++ Program to Find Sum of Digits of a Number : Calculate the sum of digits in C++ language.

C++ Program to Find Sum of Digits of a Number

#include<iostream>
using namespace std;
int main()
{
unsigned long i,p,n,sum=0;
cout<<"Enter any number:";
cin>>n;

while(n!=0)
{
p=n%10;
sum+=p;
n=n/10;
}

cout<<endl<<"Sum of digits is:"<<sum;
return 0;
}

Output of Program

Enter any number:56

Sum of digits is:11