Looping problem:
Multiply a number with the sum of its digits.
#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;
}
Comments
Leave a comment