How to get the second digit of int number in C++

1 Answer

0 votes
#include <iostream>
 
int main() {
    int number = 78594;
    int N = 2;
 
    auto s = std::to_string(number);
    std::cout << s[N - 1] << '\n';
 
    int x = s[N - 1] - '0';
    std::cout << x << '\n';
 
}
 
 
  
/*
run:
  
8
8
  
*/

 



answered May 31, 2020 by avibootz
edited Jun 1, 2020 by avibootz

Related questions

1 answer 158 views
1 answer 131 views
1 answer 170 views
1 answer 175 views
1 answer 238 views
2 answers 215 views
...