Ask a Teacher



develop program to input a program and find the sum of its digits using class and objects



#include <iostream>
void main()
{
    using namespace std;
    int num, rem, sum=0; //Declaring variables
    cout<<"Enter a number :"<<endl;
    cin>>num;
    //Loop to calculate the sum of the digits of the given number.
    while(num!=0)
    {
        rem=num%10;
        num=num/10;
        sum=sum+rem;
    }
    cout<<"Sum of the digits is "<<sum<<endl;
    cin.get( );
}


comments powered by Disqus