This exercise in numbers on a java divide to get
the number of digits that has an integer.
For example if you enter the number 3454
the program will return a 4.
import java.io. *;
class {
Figures static int total = 0;
static int x = 0;
numCifras
static int (int x) {
while (x! = 0) {x = x/10
;
total + = 1; / / increment the counter
} return total;
} public static void main (String [] args) throws IOException {
/ / BufferedReader for reading from console
BufferedReader read = new BufferedReader (new InputStreamReader (System.in));
System.out . println ("Enter a nu4mero:");
/ / read a line like string
leer.readLine String line = ();
/ * Convertirmos the string to a number.
* You could put a try and catch to see with convertirlo.Por errors
* example if you enter a string * /
x = Integer.parseInt (line);
System.out.println ("The number of digits is:");
/ / Print the number of lines
System.out.println (numCifras (x));
}}
0 comments:
Post a Comment