C++ Program to Separate Digits of a Positive Integer

Seperate digits of a positive integers 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.

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.

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.

  1. Set the variable divisor equal to 10 raised to the power of the current value of exponent.
  2. Perform integer division of number by divisor.
  3. Update the number with the remainder obtained on dividing it with divisor.
  4. 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.

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 thoughts on “C++ Program to Separate Digits of a Positive Integer”

  1. 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

  2. /*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*/


    using namespace std;

    int main(){

    int digit=0, number=0, sum, denom, length=0;
    char choose=’Y’;

    cout<<"Numbers only: ";
    cout<<"Numbers only: ";

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

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

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



    }return 0;

  3. 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..

    1. 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 ๐Ÿ™‚

    1. 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 ๐Ÿ™‚

      1. 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

Leave a Reply