2
Contributors
3
Replies
15
Views
3 Years
Discussion Span
Last Post by NathanOliver
0

I know what is Armstrong number.. i am bit confused about the logic to calculate number of digits in a number!

0

i am bit confused about the logic to calculate number of digits in a number!

What do you mean by number of didgits in a number? An armstrong number is only three digits. if you have a number xyz then x^3 + y^3 + z^3 = xyz for an armstrong number.

Are you having a hard time seperating out the digits from the start and stop numbers? If that is the case then you can break out the digits of a number as follows. note: I am storing the individual digits into an vector so they can be used later

std::vector<int> digits
int number; // this is the number we will get the digits out of example 371
while (number != 0) // once we get all of the digits stop
{
    digits.push_back(number % 10); // this gets the right most digit
    number /= 10; // this gets rid of the right most digit
}

// right now the vector holds 1, 7, 3
// if you want to have the digits in the vector reversed then use
// this needs the <algorithm> header included
std::reverse(digits.begin(), digits.end());
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.