Simple or Silly Question

Hi everybody, I just want to ask a simple(maybe a silly) question, is there a way/method/operator/function/ so I can print a units from a tens integer without if, I mean like this :

example : x = 12
then I want the output is 2,

I can make it like this
1
2
if ( x == 12 )
cout << "x = 2"


But what I want is not like that. Let's say I have a code which generate a random value(i.e from 1 - 19) for variable x, and if the value is >10, for example the value of x is 12, I want the output just to be only 2, if it's 13 the output should be 3. Is there any way I can do this without making any if ?
Because I found it's not convenient if I must make if for every tens number so the output become units.

Thanks and
I'm sorry for my silly question, or even my question is a wrong question -__-
"Beginner inside"
Last edited on
You can use the modulus operator(%) cout << "x = " << x % 10;
http://www.cplusplus.com/doc/tutorial/operators/
Use modulus
Last edited on
Recently I found this operator -= , and I use it like this
1
2
 x = 12
cout << "x = " << x -= 10
and it works nice, but soon I realize it's not to good because if my number is >20 I have the problem get revenge on me(it's only substract 10, thus the output will still tens).
But now the problems are gone, thanks to both of you.
Good day..
Topic archived. No new replies allowed.