# How to find number of digits in a number java?

20 views

## Problem:

Hello all,

I am a secondary school level student and recently started learning Java programming out my curiosity. I have got a textbook on Java programming. I just completed the chapter of for-loop and continue solving the exercises of this chapter. I have got weird a problem says to find the number of digits in a number using a java program.

Being a novice, I really don’t have any idea about this. Could anybody here answer my question, please? I know it’s might be a dumb question for you guys. But, yeah it is important to know for me. Thanks for being nice to me. I must recognize your help.

Thanks.

## Solution:

Hello Gavin,

You have got a nice question. No matter how the question is, don’t forget to ask. Thanks.
There are a couple of ways to find the number in digits in a number. As you told you just started learning and well-known with the for-loop. I am trying to solve the problem using a simple iteration. Please have a look below program. I hope the program will clarify itself to you as you’ve covered the for-loop topic already.

``````public class DigitsInNumber{
static int countDigit(long x){
int counter = 0;
while (x != 0) {
x = x / 10;
++counter;
}
return counter;
}

public static void main(String[] args){
long x = 1052809467;
System.out.print("Number of digits : " + countDigit(x));
}
}``````

Thanks again. Good Day!

## Related questions

22 views
Problem: To understand this example, you should have the knowledge of the following Java programming topics: Java Variables and (Primitive) Data Types Java while and do...while Loop Java for Loop
18 views
Question: I got an assingment in which I am asked to count the number of digits in a number through C++ program. I have tried several logics but nothing works.
4 views
Problem: Give me guidance for finding sum of digits of a number in java.
1 vote
22 views
Problem: I want to make a Java program to find the repeated digits in an integer without the use of string or array method . Can somebody do this so I can understand how to make this program? Thank you !!
1 vote
10 views
Problem: So if some one is able to shed light on this: C program to find sum of digits of a number using recursion
13 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
6 views
Problem Guide me to find the sum of all digits of a number in java.