> Create a code that should ask the user for an integer?

Create a code that should ask the user for an integer?

Posted at: 2014-12-18 
You didn't mention which language, but here's some Java code for you:

import java.util.Scanner;

public class DigitCounter {

.... public static void countDigits(int x) {

.... .... int len = (""+x).length();

.... .... System.out.printf("The number %d is %d digits long.%n", x, len);

.... } // end countDigits()



.... public static void main(String[] args) {

.... .... Scanner keybd = new Scanner(System.in);

.... .... System.out.print("Enter an integer: ");

.... .... int number = keybd.nextInt();

.... .... countDigits(number);

.... } // end main()

} // end class DigitCounter

//Note: I've used "...." in place of tabs (which Y!Answers removes).

Write a program that uses the method public static void countDigits(int x) Your program should ask the user for an integer, your program should then count how many digits are in the integer by calling countDigits() Your program should then print the message The number xxxx is yyy digits long The xxxx should be the integer the user entered and the yyy should be how many digits are in the integer.