Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class DigitCount {
- public static void main(String[] args) {
- // Create a Scanner object to read input from the user
- Scanner scanner = new Scanner(System.in);
- // Prompt the user to enter a positive integer
- System.out.print("Enter a positive integer less than 10,000,000,000: ");
- // Read the integer from the user
- long number = scanner.nextLong();
- // Close the Scanner
- scanner.close();
- // Check if the input number is within the valid range
- if (number < 1 || number >= 10000000000L) {
- System.out.println("Invalid input. Please enter a positive integer less than 10,000,000,000.");
- } else {
- // Calculate the number of digits using a loop
- int digitCount = 0;
- while (number > 0) {
- number /= 10; // Remove the rightmost digit
- digitCount++; // Increment the digit count
- }
- // Display the result
- System.out.println("The number of digits in the input is: " + digitCount);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement