+ 3

How to get each digit from the given number?

Like if the given no is 1643 and I want to access 1,6,4,3 individually

3rd Dec 2016, 9:43 AM
ankush
ankush - avatar
3 Antworten
+ 5
int digit[], digittemp[], i; int j=0; while(num>0){ digittemp[i]=num%10; num/=10; } for(i=digittemp.size()-1, I>=0, i--){ digit[j]=digit[i]; j++ } 1634 will give [1,6,3,4]
3rd Dec 2016, 9:49 AM
Sandeep Chatterjee
+ 4
To store integer in string is not simple. You will get errors. So, you can use to convert data to string like #include <iostream> #include <sstream> using namespace std; template<typename T> std::string toString(const T& value) { std::ostringstream oss; oss << value; return oss.str(); } int main() { string s = toString(1643); cout << s[2]; return 0; } output: 4
3rd Dec 2016, 11:25 AM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
Store it as a string and then you should be able to use indexes to get single characters, you can also alway parse it back into an integer, also storing it in a list of integers would be a good idea
3rd Dec 2016, 9:48 AM
Edwin Rybarczyk