how to separate an integer into a single digit

hey guys jst wanted to knw how can i separate an integer into a single digit..........e.g 12345 into '1','2',3','4','5'

closed account (28poGNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
   string str = "123456789";
   char array[str.size()];
   
   for(size_t i=0;i<str.size();i++)
        array[i] = str[i];
        
        
    for(size_t i=0;i<str.size();i++)
        cout << '\'' << array[i] << '\'' << endl;
    
   
   return 0;
}
The modulus operator can help with this assignment, e.g. 12345%10 will give you 5.
you can also use a mathematical operator...
easy to read and understand...

g=12345;

a=(g/10000);
b=(g/1000)-(a*10);
c=(g/100)-(a*100)-(b*10);
d=(g/10)-(a*1000)-(b*100)-(c*10);
e=g-(a*10000)-(b*1000)-(c*100)-(d*10);


where:
a=1
b=2
c=3
d=4
e=5
and g=12345


hoping this will help you..

Last edited on
Topic archived. No new replies allowed.