Answer to Question #345276 in C++ for Ali

Question #345276

Looping problem:

Multiply a number with the sum of its digits.


1
Expert's answer
2022-05-30T08:15:06-0400
#include <iostream>


using namespace std;
int sumOfDigits(int number){
    int sum = 0;
    while(number>9){    // if more than 1 digit
        sum+=number%10; // add the last digit
        number/=10;     // get rid of the last digit
    }
    sum+= number;  // add the only digit left
    return sum;
}
int main()
{
    int n;
    cout<<"Enter number: ";
    cin>>n;
    cout<< n*sumOfDigits(n);
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS