how to select a Digit Of Integer

closed account (ohUS216C)
Hey guys am stuck at some point
if i have a value say 5432
how to get 5?
same as the rest of the number
do i need to know the reminder??
becuase really suck at this thing
You should look into the <string> and <sstream> libraries. Using sstream, you could put your number into a string and then parse it 1 digit at a time.

String
http://cplusplus.com/reference/string/string/

Sstream
http://cplusplus.com/reference/iostream/stringstream/
5432 / 1000.

I'd bet you're more interested in getting the 2, though: 5432 % 10.
Last edited on
use %10 to get the last digit. for example if num is an int variable and num=25, then num%10=5 (the last digit of the number) and you can store that digit in a separate variable
Topic archived. No new replies allowed.