Add digits of given number - C++ Program

Q. Write a C++ program to addition of digits of any given number.

Answer:

Following program is displaying the addition of digits of a number using loops.

#include<iostream>
using namespace std;
int main()
{
        int num, rem=0, sum=0;
        cout<<"\n Enter Number : ";
        cin>>num;
        int temp=num;
        while(num>0)
        {
                rem=num%10;
                sum=sum+rem;
                num=num/10;
        }
        cout<<"\n Sum of the Digits : "<<sum;
        return 0;
}


Output:

addition of digits