• Register
2 votes
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.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes
 
Best answer

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!

13 9 6
94,260 points

Related questions

0 votes
1 answer 22 views
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
asked Nov 6, 2020 Mashhoodch 13k points
0 votes
1 answer 18 views
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.
asked Oct 13, 2020 Code Learner 9.8k points
0 votes
1 answer 4 views
4 views
Problem: Give me guidance for finding sum of digits of a number in java.
asked Apr 10 zayed1 58.7k points
1 vote
1 answer 22 views
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 !!
asked Mar 17 Raj Tyagi 14.4k points
1 vote
1 answer 10 views
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
asked Apr 26 PkGuy 23.5k points
0 votes
1 answer 13 views
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?
asked Mar 29 tuhin1 48.9k points
0 votes
2 answers 6 views
6 views
Problem Guide me to find the sum of all digits of a number in java.
asked Apr 6 salim1 60.6k points
0 votes
1 answer 48 views
48 views
I am new to this. I just want to know how to do that?
asked Nov 6, 2020 TeamScript 13.5k points
0 votes
1 answer 2 views
2 views
Problem: What characters are valid in a Java class name? What other rules govern Java class names (for instance, Java class names cannot begin with a number)?
asked Apr 26 anika11 32k points
1 vote
1 answer 8 views
8 views
Problem: How to make a Java program to find sum of digits of an integer , for eg. I the integer is 541 then the output should be the sum of these digits : 5+4+1 = 10.
asked Mar 17 Raj Tyagi 14.4k points