digit program

Write a program that reads an integer and prints its digits. For instance, if the user
enters -341670, the program should print the following lines:
Digit 1 is: 3
Digit 2 is: 4
Digit 3 is: 1
Digit 4 is: 6
Digit 5 is: 7
Digit 6 is: 0

so far i got this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;

int main() {

	int n,d;
	cout << "Enter the number.";
	cin >> n;
	if ( n == 0) 
	d=1;
	else 
	d = 1 + log10(abs(n));
	cout << n << " has " <<d <<" digits."<<endl;
I'm not sure what you were trying to do with abs and logs. The modulus method would be the best. Remember the digits you are talking about are from the decimal base. So if we use the modulus of 10 ( decimal ) that will give us the right most digit. We can then remove that digit by dividing by 10 again its in decimal base.

Here would be a simple way to do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
	int input = 0;
	int count = 1;
	
	std::cout << "Please enter a number: " << std::endl;
	std::cin >> input;
	
	if( input < 0 )//negative
		input = - input; //negate or absolute value
		
	while( input > 0 ) //loop until there are no digits left
	{
		std::cout << "Digit " << count << " is: " << input % 10 << std::endl; //outputting right digit
		input /= 10; //removing right digit
		++count; //incrementing the counter
	}
}


Oh you want it in order from left to right. What you want to do then is to store the digits in an array then output the array.

Another method which might be easier for you would be to input a string then output each digit though your assignment wants to input an int.

Another method would be to input an integer like assignment says then to stream it to a string then output each digit here is an example:

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
#include <iostream>
#include <sstream>
#include <string>

int main()
{
	int input = 0;
	std::string number = "";
	std::stringstream ss;
	
	std::cout << "Please enter a number: ";
	std::cin >> input;
	std::cout << input << std::endl; //so it looks neater on ideone.com/3biksj
	
	if( input < 0 ) //negative
		input = - input; //negate
	
	ss << input;
	ss >> number;
	
	const int SIZE = number.size();
	
	for( int i = 0; i < SIZE; ++i )
	{
		std::cout << "Digit " << i + 1 << " is : " <<  number[i] << std::endl;
	}
}


If you still need help let me know.
is there a simpler way to do this cuz i dont think we learn array yet.. or any method u posted.
and i was given a hint:
Find the length of the integer first, that is the number of digits. In the above case, the length of -341670 is 6. Then to get the first digit compute 341670 / 10^5 = 3. Then 341670 - 3*10^5 = 41670. Now apply the above steps to 41670.
Last edited on
yeah that would work too. Look like d is your amount of digits

So from there you want to follow the formula provided it would look like this

1
2
3
4
5
6
7
8
int last_digit = 0;
int power = pow( 10 , d );
for( int i = 0; i < d; ++i )
{
    last_digit = n / power;
    n -= last_digit * power;
    std::cout << "Digit " << i + 1 << " is : " << last_digit << std::endl;
}

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
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;

int main() {

	int n,i,d;
	cout << "Enter the number.";
	cin >> n;
	if ( n==0)
	d=1;
	else 
	d=1 + log10(abs(n));
	cout << n << " has " << d <<" digits."<<endl;	
	
	int digit=0;
	int power= pow(10,d);
	for (int i=0; i < d; i++) {
		digit = n / power;
		n -= digit * power; 
		cout << "Digit " << i+1 << " is: " << digit<<endl;
	}
	
	return 0;
	}


what am i doing wrong? it keeps printing zeros for the digits.
Last edited on
The power of 10 must be modified by -1 inside of the loop. Something like this:

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
33
34
35
36
37
38
#include <iostream>

using namespace std;
int p10(int);

int main()
{
	int number, digit;
    cout << "\nEnter a number: ";
    cin >>number;
    if(number < 0)
		number = - number;
	if(number == 0)
		cout << "Digit 1 is : 0\n";
	int temp =number;
    int k = 0; 
    while(temp > 0)
    {
		temp /= 10;
		++k; 
	}
	int j = k - 1;
	for( int i = 0; i < k; ++i )
	{
		digit = number / p10(j);
		number -= digit * p10(j);
		cout << "Digit " << i + 1 << " is : " << digit << endl;
		--j;
	}
}

int p10(int i)
{
	int val = 1;
	for(int k = 0; k < i; ++k)
		val *= 10;
	return val;
}

Output sample:
1
2
3
4
5
6
7
Enter a number: 548256
Digit 1 is : 5
Digit 2 is : 4
Digit 3 is : 8
Digit 4 is : 2
Digit 5 is : 5
Digit 6 is : 6
Last edited on
Sorry that was my fault. line 18 should be d - 1 instead of d. Reason being to find the left digit in 100 we would divide by 10 to the second not ten to the third. 10^ == 100 and 100 / 100 = 1. Also don't forget if it is a negative value to negate it to a positive. ex -341670 you wan't to make it 341670 the negative sign doesn't affect the digits but it will mess up your calculations. Also line 11 should be if n < 10 instead of == 1 after you negate.

Also after checking the formula you mentioned earlier it should be


10^digits - position - 1


so it would look something like this

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>
#include <cmath>
using namespace std;

int main() {

	int n,i,d;
	cout << "Enter the number.";
	cin >> n;
	
	if( n < 0 )
		n = - n;
		
	if ( n < 10 )
		d=1;
	else 
		d = 1 + log10(abs(n));
	cout << n << " has " << d <<" digits."<<endl;	
	
	int digit=0;
	int power = 0;
	
	for (int i=0; i < d; i++)
	{
		power = pow( 10 , d - 1 - i );
		digit = n / power;
		n -= digit * power; 
		cout << "Digit " << i+1 << " is: " << digit<<endl;
	}
	
	return( 0 );
}


Though might I suggest you use better variable names instead of d and n.
Maybe try input for n and number_of_digits for d?

Topic archived. No new replies allowed.