Count Digits in a Number
This program counts the number of digits in a number using while loop.
Problem Statement
Write a Java program that counts digits in 98765 using while loop.
Source Code
| 1 | public class CountDigits { |
| 2 | public static void main(String[] args) { |
| 3 | int num = 98765, count = 0; |
| 4 | while(num > 0) { |
| 5 | count++; |
| 6 | num /= 10; |
| 7 | } |
| 8 | System.out.println("Number of digits = " + count + " - Eduinq"); |
| 9 | } |
| 10 | } |
Program Output
Number of digits = 5 - Eduinq
Explanation
The program divides the number by 10 repeatedly until it becomes 0, incrementing count each time. This demonstrates how while loops can process digits sequentially to determine the total count. Shows how while loop can count digits in a number.