separating individual digits from an integer

hi..

Can someone help me.. I am trying to improve my C++. It is so bad.I cant even do this.

I need to read an integer and then separate them into individual digits. I tried looking in the forums..but some of the solutiosn involve array and a static limit.

I need something more like..any mumber of integers, it can consist of 2 digits or 10 digits. For example, if I enter 8272, the program should display 8 2 7 2 and if I key in 75602, it should display 7 5 6 0 2 and then finally get the sum..

But I need to know how to get it to output the individual digits. I have created the following program, but it only outputs the last digit


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>

using namespace std;



int main()
{
	int enter_num, division=0;
	int remainder;

	cout<<"Please enter numbers"<<"\n";
	cin>>enter_num;

	do
	{			
		remainder=enter_num%10;
		division=enter_num/10;
		cout<<remainder<<"\n";	
	}
	while(division=0);

	

	return 0;
}


Please help...I keep trying and trying..
Now just why would you want to do that?

Either way... I recommend a for loop which divides your number by decreasing powers of ten (but do not change the original number), and stores that in an element of an integer array.

http://cplusplus.com/doc/tutorial/arrays/

You will also need a method of checking how large your number is. Try the reverse of the above that checks if number/poweroften is greater than one. Use a floating point number for this check.

-Albatross
The condition for the do-while loop, on line 21, is assigning 0 to division and resulting in false, which terminates the loop. Fix that condition and you'll be on your way to figuring this one out.
Looks like some practice assignment from some book or something. Nice to see you've tried alot. Problemsolving skill is something that grows on you, don't worry. And never be afraid to ask :)
hi..guys..
well.. I managed to come up with something, but I kinda converted the do while.. to while..

but somehow..I am getting the numbers reversed..arghhh..need some help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream> 
using namespace std; 
 

int main() 
{
	int num,counter,limit,division; 
	 
	cout<<"Please enter how big the set will be"<<"\n"; 
	cin>>limit;
	cout<<"Enter the integers please"<<"\n"; 
	cin>>num;

	counter=0;

	while(counter<limit) 
	{
		division=num/10;
		cout<<num%10<<" ";
		num=division;
		counter++;
	}
	return 0; 
}

Last edited on
You could also read the individual digits as chars.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	std::string buffer;
	std::cin >> buffer;

	for(unsigned int i = 0; i < buffer.size(); ++i)
	{
		if(!isdigit(buffer[i]))
		// deal with bad input and leave the loop

		std::cout << buffer[i];
		if(i != buffer.size() - 1) // no space required after the last digit
		{
			std::cout << ' ';
		}
	}
Last edited on
Hi thanks for the input

this program is actually ane exercise froma book, and its from the control structures chapter..and I need to use while loop to do it..

i have not gone to arrays part yet...

other solutions..using while ...?
try this

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

int main()
{
     int num;
     std::cout << "Please enter a number";
     std::cin << num;

     while(num > 0)
     {
          std::cout << num%10 << std::endl;     // Output the last digit
          num /= 10;                      // Divides the number by 10 i.e. removes the last digit
     }
     std::cin.get();
     return 0;
}

@kalp1200
Yes, you get them reversed because you are getting the digits from least-significant to most-significant, and printing them in that order.
However, humans read numbers from most- to least-significant order.

123 --> 12 and 3 (print 3)
12 --> 1 and 2 (print 2)
1 --> 0 and 1 (print 1)

printed: 321

The way to fix it is either:
1) put the numbers you get into a vector or something and print them in reverse order
2) get the digits from most-significant to least (instead of least to most as you are doing now).

Good job and good luck!
Hi everyone, i think I have solved the question.

But mine is very long...now that i have solved..can anyone send me the shorter version of it..
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*Outputs the digit*/

#include<iostream>
#include<cmath>

using namespace std;



int main()
{
	int numbers,numbers1,answer,i;
	int size,sum=0;
	double times;

	cout<<"Please enter how big the set will be"<<"\n";
	cin>>size;

	cout<<"Enter the integers please"<<"\n";
	cin>>numbers;

	if(numbers<0)
	{
		for(i=size;i>0;i--)
		{
			times= pow(10.0,size-1);
			//cout<<times<<"\n";
			static_cast<int>(answer)=-(static_cast<int>(numbers))/static_cast<int>(times);
			cout<<answer<<" ";
			static_cast<int>(numbers1)=static_cast<int>(numbers)%static_cast<int>(times);
			sum = sum + answer;
			//cout<<numbers1<<"\n";
			numbers=numbers1;
			size--;
		}
	}

	else 
	{
		for(i=size;i>0;i--)
		{
			times= pow(10.0,size-1);
			//cout<<times<<"\n";
			static_cast<int>(answer)=static_cast<int>(numbers)/static_cast<int>(times);
			cout<<answer<<" ";
			static_cast<int>(numbers1)=static_cast<int>(numbers)%static_cast<int>(times);
			//cout<<numbers1<<"\n";
			sum = sum + answer;
			numbers=numbers1;
			size--;
		}
	}


	cout<<"The sum of the numbers are "<<sum;

	return 0;
}
This one is smaller...

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
int main()
{
   int enter_num, temp_num, sum = 0;
   int divisor, digit, count = 0;

   cout<<"Please enter number"<<"\n";
   cin>>enter_num;

   temp_num = enter_num;

   // Counting the number of digits in the entered integer
   while (temp_num != 0)
   {
       temp_num = temp_num/10;
       count++;
   }

   temp_num = enter_num;

   // Extracting the digits
   cout<<"Individual digits in the entered number are ";
   do
   {
       divisor = static_cast<int>(pow(10.0, --count));
       digit = temp_num / divisor;
       temp_num = temp_num % divisor;

       cout<<" "<<digit;
       sum = sum + digit;
   }
   while(count != 0);

   cout<<"\n"<<"Sum of the digits is = "<<sum<<"\n";

   return 0;
}
Last edited on
Topic archived. No new replies allowed.