How can I split an int in c++ to its single numbers? For example, I'd like to split 23 to 2 and 3.
- 9Think of it as a brainteaser. If you still can't work out how to do it, post what you tried and people can comment.Alex Brown– Alex Brown2010-11-23 22:25:11 +00:00Commented Nov 23, 2010 at 22:25
- 2possible duplicate of Split an Integer into its digits c++Steve Townsend– Steve Townsend2010-11-23 23:14:49 +00:00Commented Nov 23, 2010 at 23:14
- try yourself ,and if you find a different way ,share it with us.Prashant Verma– Prashant Verma2025-08-15 16:57:57 +00:00Commented Aug 15 at 16:57
17 Answers
Reversed order digit extractor (eg. for 23 will be 3 and 2):
while (number > 0) { int digit = number%10; number /= 10; //print digit } Normal order digit extractor (eg. for 23 will be 2 and 3):
std::stack<int> sd; while (number > 0) { int digit = number%10; number /= 10; sd.push(digit); } while (!sd.empty()) { int digit = sd.top(); sd.pop(); //print digit } 2 Comments
The following will do the trick
void splitNumber(std::list<int>& digits, int number) { if (0 == number) { digits.push_back(0); } else { while (number != 0) { int last = number % 10; digits.push_front(last); number = (number - last) / 10; } } } 1 Comment
number /= 10 in code like this.A simple answer to this question can be:
- Read A Number "n" From The User.
- Using While Loop Make Sure Its Not Zero.
- Take modulus 10 Of The Number "n"..This Will Give You Its Last Digit.
- Then Divide The Number "n" By 10..This Removes The Last Digit of Number "n" since in int decimal part is omitted.
- Display Out The Number.
I Think It Will Help. I Used Simple Code Like:
#include <iostream> using namespace std; int main() {int n,r; cout<<"Enter Your Number:"; cin>>n; while(n!=0) { r=n%10; n=n/10; cout<<r; } cout<<endl; system("PAUSE"); return 0; } Comments
cast it to a string or char[] and loop on it
3 Comments
the classic trick is to use modulo 10: x%10 gives you the first digit(ie the units digit). For others, you'll need to divide first(as shown by many other posts already)
Here's a little function to get all the digits into a vector(which is what you seem to want to do):
using namespace std; vector<int> digits(int x){ vector<int> returnValue; while(x>=10){ returnValue.push_back(x%10);//take digit x=x/10; //or x/=10 if you like brevity } //don't forget the last digit! returnValue.push_back(x); return returnValue; } 3 Comments
Declare an Array and store Individual digits to the array like this
int num, temp, digits = 0, s, td=1; int d[10]; cout << "Enter the Number: "; cin >> num; temp = num; do{ ++digits; temp /= 10; } while (temp); for (int i = 0; i < digits-1; i++) { td *= 10; } s = num; for (int i = 0; i < digits; i++) { d[i] = s / td %10; td /= 10; } Comments
int n = 1234; std::string nstr = std::to_string(n); std::cout << nstr[0]; // nstr[0] -> 1 I think this is the easiest way.
We need to use std::to_string() function to convert our int to string so it will automatically create the array with our digits. We can access them simply using index - nstr[0] will show 1;
Comments
Start with the highest power of ten that fits into an int on your platform (for 32 bit int: 1.000.000.000) and perform an integer division by it. The result is the leftmost digit. Subtract this result multipled with the divisor from the original number, then continue the same game with the next lower power of ten and iterate until you reach 1.
Comments
I don't necessarily recommend this (it's more efficient to work with the number rather than converting it to a string), but it's easy and it works :)
#include <algorithm> #include <iostream> #include <iterator> #include <string> #include <boost/lexical_cast.hpp> int main() { int n = 23984; std::string s = boost::lexical_cast<std::string>(n); std::copy(s.begin(), s.end(), std::ostream_iterator<char>(std::cout, "\n")); return 0; } Comments
You can count how many digits you want to print first
#include <iostream> #include <cmath> using namespace std; int main(){ int number, result, counter=0, zeros; do{ cout << "Introduce un numero entero: "; cin >> number; }while (number < 0); // We count how many digits we are going print for(int i = number; i > 0; i = i/10) counter++; while(number > 0){ zeros = pow(10, counter - 1); result = number / zeros; number = number % zeros; counter--; //Muestra resultados cout << " " << result; } cout<<endl; }
Comments
int power(int n, int b) { int number; number = pow(n, b); return number; } void NumberOfDigits() { int n, a; printf("Eneter number \n"); scanf_s("%d", &n); int i = 0; do{ i++; } while (n / pow(10, i) > 1); printf("Number of digits is: \t %d \n", i); for (int j = i-1; j >= 0; j--) { a = n / power(10, j) % 10; printf("%d \n", a); } } int main(void) { NumberOfDigits(); } Comments
#include <iostream> using namespace std; int main() { int n1 ; cout <<"Please enter five digits number: "; cin >> n1; cout << n1 / 10000 % 10 << " "; cout << n1 / 1000 % 10 << " "; cout << n1 / 100 % 10 << " "; cout << n1 / 10 % 10 << " "; cout << n1 % 10 << " :)"; cout << endl; return 0; }