Question

Use an endless loop structure (with a sentinel escape) to count the number of digits in...

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**

Homework Answers

Answer #1
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);
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
8.36 Lab 8a: Count by 3 Introduction For this lab, you will use a while loop...
8.36 Lab 8a: Count by 3 Introduction For this lab, you will use a while loop to complete the task. A while loop has this basic structure: /* variable initializations */ while (/*stop condition*/){ /* statements to be performed multiple times */ /* make sure the variable that the stop condition relies on is changed inside the loop. */ } Despite the structure of the while loop being different than that of a for loop, the concept behind it is...
write a C program to find whether the given number of three digits is an integer...
write a C program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or -999 to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or -999 to quit : 371 3**3 + 7**3 + 1**3 is = 371...
Create a program that allows the user to input a list of first names into one...
Create a program that allows the user to input a list of first names into one array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. The output should be a list of emial addresses where the address is of the following form: [email protected] Declare FirstName[100] as String Declare LastName[100] as String Declare email as String Declare K as Integer Declare index as Integer Write "Enter first and last name." Write...
Using a while loop, write a program that reads 10 integer numbers. The program shall count...
Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below. c++ language
C++ code please: Write a program to count the number of positive and the number of...
C++ code please: Write a program to count the number of positive and the number of negative inputs values. The program will stop when a 0 is input. Ex: If the input is -5.5 568 2.332 0 the output is 2 positive number(s) and 1 negative number(s). Ex: If the input is 153.0 0.534 2.2 5.6 46.584 0.015 5 0 the output is 7 positive number(s) and 0 negative number(s).
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0...
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0 if(x<1): print('Error') exit() print('Initial value is: ',x,' ',end='') while(x!=1): if(x%2==0): x=x/2 else: x=3*x+1 count+=1 if(x!=1): rint('Next value is: ',int(x),' ',end='') else: print('Final value ',int(x),', ',end='') print('number of operations performed ',int(count),' ',end='') break
in c++ Write a C++ program that asks the user to enter an integer number and...
in c++ Write a C++ program that asks the user to enter an integer number and prints it back "vertically" to the screen. Use recursion in your solution. As an example of how the program will work: Please enter an integer: 12345 The integer you entered will print vertically as: 1 2 3 4 5
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display...
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display 5 lines of smiley faces. The following would be the output of your program: Note: Sample program is in the "Important Documents", Module 6 folder) Hello:                          Bye!
C++ please Write a program that allows a user to enter any number of product prices...
C++ please Write a program that allows a user to enter any number of product prices and show the total of all of the products. Use 0.00 as a sentinel value. (while loop)
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail. Example of the expected output is as follows: