Below is a simple Java program to count the number of digits in a given number:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
long number = scanner.nextLong();
int count = 0;
// Counting the number of digits
while (number != 0) {
// Increment count for each digit
count++;
// Remove the last digit from the number
number /= 10;
}
System.out.println("Number of digits: " + count);
scanner.close();
}
}
This program takes a number as input and counts the number of digits in it. It repeatedly divides the number by 10 and increments the count until the number becomes 0. Finally, it prints out the count.