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 143 views
1 answer 117 views
1 answer 154 views
1 answer 159 views
1 answer 115 views
1 answer 215 views
...