# C++ Program to Separate Digits of a Positive Integer 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. 🙂

This site uses Akismet to reduce spam. Learn how your comment data is processed.

## 16 Responses on this post

1. Ch Ahmed says:

Write a C++ program that takes input a number greater than 9999 and less than 100000. Then breaks every digit of the number and adds them separately in another variable. Then prints that variable. ( without using anything while,loopetc)

2. Mohamed Mahmoud says:

// this is easy than you type .

#include
using namespace std;
int main()
{
int n; cin >> n;
for (int ts = 1; ts > num;
int copy_num = num ;

while (num != 0) {

int r = num % 10;
count++;
num /= 10;
}

while (count > 1) // 3 5 1
{
int r = copy_num % 10;
cout << r << " ";
count–;
copy_num /= 10;
}
cout << copy_num<<"\n";

}

system("pause");

return 0;
}

3. zain ul abadeen says:

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

1. Muhammad Umer Mansoor says:

Here you go brother @zain ul abadeen

#include

using namespace std;

int main()
{
long num,dig1,dig2,dig3,dig4,dig5,dig6,sumnum;
cout <> num;
if (num > 999999)
{
cout << "Error: Please enter a SIX digit number!";
}
else
{
dig6 = num%10;
dig5 = num/10%10;
dig4 = num/100%10;
dig3 = num/1000%10;
dig2 = num/10000%10;
dig1 = num/100000%10;
sumnum = dig1 + dig2 + dig3 + dig4 + dig5 + dig6;
cout << "The sum of " << dig1 << " + " << dig2 << " + " << dig3 << " + " << dig4 << " + " << dig5 << " + " << dig6 << " = " << sumnum << ".";
}
return 0;
}

4. sayed muzammil ali says:

Thank you for zubair bhai

5. Muhammad AlAshari says:

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

#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;
}

6. Mian Anas says:

Great efforts bro . . . .

1. Muhammad Anas says:

Thank you 🙂

7. Abdulkarim Nasir says:

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

8. Raja fahad ahmed says:

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. Muhammad Anas says:

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 🙂

9. ashwin says:

how to do this using for loop
???????

1. Muhammad Anas says:

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. khurram says:

A.SALLAM

ANAAS BI

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

10. zubair says:
1. Muhammad Anas says: