'print the nth digit of a number in JAVA

I want to print nth digit of a number (from left to right) in JAVA. Here's what I've tried, I'm a total noob.

public class NewClass { public static void main(String args[]) {

    System.out.println("enter number: ");
    Scanner s = new Scanner(System.in);

    int number = s.nextInt();

    System.out.println("enter n: ");
    Scanner n = new Scanner(System.in);

    int num = n.nextInt();


    for (int i = 1; i <= num; i++) {

        number = number / 10;
    };
   System.out.println("The nth Digit = " + number);
}


Solution 1:[1]

Following @ryan 's explanation, I found a solve in my way.

package com.mycompany.lab;
import java.util.Scanner;

int reversed = 0, digit;

System.out.println("enter number: ");
Scanner s=new Scanner(System.in);
int number=s.nextInt();

while(number != 0) {
    
  // get last digit from num
  digit = number % 10;
  reversed = reversed * 10 + digit;

  // remove the last digit from num
  number /= 10;
}

System.out.println("enter n: ");
Scanner n=new Scanner(System.in);

int num=n.nextInt();

for(int i=1;i<num;i++){

reversed = reversed / 10;
};

while(reversed > 10){
    reversed = reversed % 10;
}
    
System.out.println("The nth Digit = " + reversed);
}  
}  

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sabbir Hasan