Suppose you want to write a program in C++ which will take a positive integer from the user and output its individual digits. For example, if the input provided is 12345 then it will print 1,2,3,4 and 5 separately as integers. In this article we shall look at a few different ways of accomplishing exactly this.

First of all, look at the following program. It is probably the simplest way of separating digits of an integer. It makes use of the fact that when the division operator (/) is used with two integer operands then it gives us only the whole part of the quotient and the remainder operator (%) gives us the remainder when dividend is divided by the divisor. In this particular case, we use 10 with both these operators as divisor. When we use it with the remainder operator we get the last digit of the number entered by the user and when we use it with the division operator then we get the original number without that last digit. In this way, repeating these two steps until the original number becomes zero, we get each individual digit of the original number form right to left i.e. from least significant to most significant digit.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; int main() { int number; cout << "Enter a number to separate it's digits: "; cin >> number; while (number != 0) { cout << number%10 << " "; number /= 10; } cout << endl; system("pause"); return 0; } |

But the problem with this program is that it gives us the digits of the number in reverse order as they appear in the number. If you want to get the digits in the same order as they appear in the original number then have a look at the following two programs.

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> using namespace std; int main() { int number; cout << "Enter a number to separate it's digits: "; cin >> number; int exponent = floor( log10( static_cast<double>(number) ) ); int divisor; while (number != 0) { divisor = pow(10.0, exponent); cout << number/divisor << " "; number %= divisor; exponent--; } cout << endl; system("pause"); return 0; } |

This program first detects the number of digits in the number entered and stores one less than it in a variable. The fact used to find the number of digits in the number is that **the number of digits in a positive integer is the smallest integer greater than it’s common logarithm.** If we apply the *ceil()* function to it’s logarithm then we will get exactly this number but as I said earlier, we actually need the number one less than it, so we apply the function *floor()* to it instead and store the result in the variable *exponent*. After that we repeat following steps until the number becomes zero.

- Set the variable
*divisor*equal to 10 raised to the power of the current value of*exponent*. - Perform integer division of
*number*by*divisor*. - Update the
*number*with the remainder obtained on dividing it with*divisor*. - Decrement
*exponent*.

In this way, we get the digits of a number in exactly the same order as they appear in the original number.

And here is another program which does exactly this but in a slightly different manner.

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 |
#include <iostream> using namespace std; int main() { int number; cout << "Enter a number to separate it's digits: "; cin >> number; int reverseNum=0; while (number > 0) { reverseNum += (number%10); reverseNum *= 10; number /= 10; } reverseNum /= 10; while (reverseNum != 0) { cout << reverseNum%10 << " "; reverseNum /= 10; } cout << endl; system("pause"); return 0; } |

This program first reverses the order of digits in the entered number and stores it in another variable *reverseNum* and then applies the same technique of the first program in this article to separate it’s digits and output them. The method with which it builds the number containing digits of another number in reverse order is that; get the digits from the original number from right to left one by one, add them to the number which will be initialized at 0 and also multiply it by 10 on each step. In this way the digit at the least significant position in the original number becomes the most significant digit in the resulting number and vice versa. But this process gives us ten times large number than the reverse of the original number. To overcome this problem we divide the number by 10 once after coming out of the *while loop*. After all this, we send this reversed number to the exact same while loop used in the first program of this article and we are done.

**Warning:** The last two programs discard any zero’s appearing at the right most (least significant) positions of the number. For example, they output 1 2 3 for 12300 and 1 for 1000 etc. while the first program doesn’t ignore these right most zero’s though it outputs them in the reverse order. ๐

## 13 replies on “C++ Program to Separate Digits of a Positive Integer”

1. Write and run a program that reads a six-digit integer and prints the sum of its six digits.

Sample Input: 153426

Sample output: 21

plz solve this solution

Thank you for zubair bhai

/*Studentโs full name: Mohammad Mushfequr Rahman

Studentโs ID: 4704265

Modification Date: 20/04/2014 (DD/MM/YY)

Purpose of this file (or program): this program disects a number

Answer 1*/

#include

using namespace std;

int main(){

int digit=0, number=0, sum, denom, length=0;

char choose=’Y’;

while(choose==’Y’){

denom=1;

sum=0;

cout<>number)){

cout<<"Numbers only: ";

cin.clear();

cin.ignore();}

cout<>length)){

cout<<"Numbers only: ";

cin.clear();

cin.ignore();}

for(int i=1; i<length;i++){

denom=denom*10;

} cout<<"Modulo and Diviser is: "<<denom<<endl;

cout<0){

digit=number/denom;

cout<<digit<<" ";

sum=digit+sum;

number=number%denom;

denom=denom/10;

}cout<<"sum "<<sum<<endl;

cout<>choose;

if(choose==’N’){

break;}

}return 0;

}

Great efforts bro . . . .

Thank you ๐

Good job bro.This site is really helpful.Please i need a tutorial in strings…..Thanks and God bless

Dear how to make it easy?? i want to learn it easily what’s happening here exactly.. i am a new programmer learning it. so plz tell me bro..

yes sure bro. just let me know exactly at which line or step are you struggling to understand? I will try my best to explain it in simple and easy way to you. Thanks for visiting ๐

how to do this using for loop

???????

well …

1) why you want to do it using for loop specifically??

2) a for loop can easily be used in place of a while loop by leaving the 1st and 3rd statement in its header empty e.g.

while (number != 0)

{

cout << number%10 << " "; number /= 10; } can be replaced with: for ( ; number != 0; ) { cout << number%10 << " "; number /= 10; } But honestly I don't see any point in doing so because this is exactly the situation for which while loop is meant ๐

A.SALLAM

ANAAS BI

i am new in this c++ i dont know even how this program will work by using for loop..

please help me

nice anas bhi……..!

Thanks for liking Zubair bhai ๐