+ 2

If we have number in integer let x we have to find all the digits of the number and store in different variables

For example: x=1234 Then a=1 b=2 c=3 d=4 Other example:x=786 a=7 b=8 c=6 And X is any number

19th Dec 2019, 8:33 PM
Himanshu Sharma
Himanshu Sharma - avatar
1 Antwort
0
Declare and initialise you variables "a,b,c" etc to 1 or 0 (it depends on what you intend to do with them after) Convert your "number" to a string. Use a switch with the string length as the expression. For the "cases"...here's an example:- case 1: a = stoi(mystring.substr(0, 1)); break; case 2: a = stoi(mystring.substr(0, 1)); b = stoi(mystring.substr(1, 1)); break; .....etc. ..is there a better way?...I don't know...that's the best I could come up with.
19th Dec 2019, 11:23 PM
rodwynnejones
rodwynnejones - avatar