+ 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
3 Answers
+ 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]
+ 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
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