Use an endless loop structure (with a sentinel escape) to count the number of digits in any input integer.
Sample output: Enter the integer ( -99 to exit) : 16
16 has 2 digits
Enter the integer ( -99 exit): 12345
12345 has 5 digits
Enter the integer (-99 to exit): -99
**Write program in JAVA**
import java.util.Scanner; public class DigitsCount { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; do { System.out.print("Enter the integer (-99 to exit): "); n = in.nextInt(); if (n != -99) { int count = 0; int num = n; while (num > 0) { ++count; num /= 10; } System.out.println(n + " has " + count + " digits"); } } while (n != -99); } }
Get Answers For Free
Most questions answered within 1 hours.