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

In this program, the user is ask to enter the number, then the sum of digits is calculated and displayed on the console.

For example, if the user enter 1234 then the sum of digits will be
1 + 2 + 3 + 4 = 10



Source Code
#include<iostream>
using namespace std;

int main()
{
    int num, sum=0;
	
    cout<<"Enter a number: ";
    cin>> num;

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

    cout<<"Sum of digits = " << sum;
    return 0;
}
Output
Enter a number: 4562
Sum of digits = 17