Outputting an integer individually?

Can someone help me make a program where the user enters in an enter and the program outputs the individual digits.

For instance, 3456 is out putted as 3 4 5 6, 8030 is outputted as 8 0 3 0
For this you will have to use some math.

We use the decimal base so this is really
3 * 103 + 4 * 102 + 5 * 101 + 6 * 100

Basically to output digits you'll have to do something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>


int i_pow( int number , const int n );

int main()
{
	const int 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 , const int 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.

http://ideone.com/bZ3FIL

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.
instead of using the constant variable try
1
2
3
4
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.
Topic archived. No new replies allowed.