C++ program to find the sum of the digits of a given number

#include <iostream>
using namespace std;
int main()
{
    int n=12345,digit,sum=0;
    while(n!=0)
    {
        digit=n%10;
        sum+=digit;
        n=n/10;
    }
    cout<<"Sum of ALL digits is: "<<sum;
    return 0;
}

No comments:

Post a Comment