xxxxxxxxxx
public class Main {
public static void main(String[] args) {
int number = 98765;
int digitIndex = 3; // The index of the digit to be extracted, starting from right (0-indexed)
int digit = getDigit(number, digitIndex);
System.out.println("The digit at index " + digitIndex + " is: " + digit);
}
public static int getDigit(int number, int digitIndex) {
// Convert the number to a string
String numberString = String.valueOf(number);
// Get the index of the digit from the right
int reversedIndex = numberString.length() - 1 - digitIndex;
// Extract the digit at the given index
char digitChar = numberString.charAt(reversedIndex);
// Convert the digit character to an integer and return it
return Character.getNumericValue(digitChar);
}
}
xxxxxxxxxx
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int count = 0;
while (num > 0) {
num /= 10;
count++;
}
System.out.println("number of digits: " + count);
}