In general, I ran into a problem: I want to create a small program in which the user enters any integer number and the length of this number is displayed on the screen, i.e. the number of digits of which this number consists. There is only one nuance: the variable into which we enter our number must be of type int. So, I need to enter this number, convert it to an array of type char, and display the length of the number.
1 answer
If you have an array of integers (or a single integer), then print the lengths of the symbolic representations of the numbers simply, for example,
int[] a = { 10, 5, 543, 87654 }; foreach (int x in a) Console.WriteLine("{0}: {1}", x, x.ToString().Length); The output of this code snippet will be as follows.
10: 2 5: 1 543: 3 87654: 5 The only problem is when the number is negative. In this case, you must check whether it is less than zero, and if it is, then subtract one from the expression x.ToString().Length .
- 2Or
Math.Abs(x).ToString().Length- Qodirbek Makharov
|