I need to find a digit at a specific postion in a number. Example number 652796, user inputs a 4, "2" is displayed.
I have the basis for it, but most of the time, the output is incorrect. Not sure what's wrong.
#include <iostream>
#include <cmath>
using std::cout;
using std::cin;
int main ()
{
int number = 0,
digits = 0,
position = 0,
result = 0;
cout << "Enter a number: ";
cin >> number;
cout << "Enter a digit position:";
cin >> position;
if ( 0 == number)
{
cout << "0";
}
while (position-- > 1)
{
number = number / pow(10,position);
result = number % 10;
}
cout << result <<'\n';
return 0 ;
}