#include <iostream>
int i_pow( int number , constint n );
int main()
{
constint number = 3456;
int digit = 0 , size = 0 , temp = number , power = 0;
while( temp > 0 )
{
++size;
temp /= 10;
}
temp = number;
for( int i = 0; i < size; ++i )
{
power = i_pow( 10 , size - 1 - i );
digit = temp / power ;
temp -= power * digit;
std::cout << "Digit " << i << " = " << digit << std::endl;
}
}
int i_pow( int number , constint n )
{
if( n > 0 ) return( number * i_pow( number , n - 1 ) );
return( 1 );
}
You can use the cmath pow function also but they use a very complex function and to find digits we can use the easy version where basically it is the number times it self for n times.
Gi lit, thank you for your response :) didn't clarify this before, but suppose a user is suppose to enter in a number. How would I do it then? Also, I'm just learning basics in my class right now :) and this question was on the chapter of loops.
int number = 0;
std::cout << "Please enter a number: ";
std::cin >> number;
If you don't understand the code I can explain. But basically it is getting a number , then it finds the size of the number ( by dividing by 10 until the number = 0 ) reason the number gets to zero is because integer divided by integer is integer so if the number is like 8 then 8 / 10 = .8 = 0 in integer form. Then It loops for the digits and the digit is equal to the number divded by 10 * position - 1 Like I mentioned earlier its decimal. Then you subtract that value from the larger number to remove the left hand digit.