• Register
2 votes
14 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,240 points

Related questions

0 votes
1 answer 14 views
14 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 6.5k points
0 votes
1 answer 18 views
18 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 10.2k points
0 votes
1 answer 39 views
39 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 9 views
9 views
How can I get it in Java?
asked Jan 22 TeamScript 13.5k points
0 votes
1 answer 4 views
4 views
Problem: How to write a program that finds the duplicate words and their number of occurrences in a string? I am a beginner with no prior experience in programming so facing some difficulty in developing logic. Please help. Thanks!
asked 4 days ago Code Learner 6.5k points
0 votes
1 answer 12 views
12 views
Problem: How to get number of digits in an integer? What is the acutal way to do this?
asked Nov 3, 2020 Mubashi-Question 1.7k points
1 vote
1 answer 456 views
456 views
Problem : I am very new to programming. I need to write a program called swapAdj that must accept the array of integers and after that swap those elements at the adjacent indexes. For example consider the elements 0 and 1 are swapped by the program and after ... note if your array has the odd length then the program must keep final element unmodified. Please help me in writing above program.
asked Apr 7, 2020 morrisBson 3.2k points
0 votes
1 answer 187 views
187 views
Problem: Hi dear friends is there any way to find out the sum of total element of array which is equal to given number. Please suggest any example.
asked Nov 6, 2020 Mubashi-Question 1.7k points
0 votes
1 answer 17 views
17 views
Problem: Hello Kodlogs! I am a beginner and started learning programming through an online course. In course, I am given an assignment in which I have to write a program that will count the occurrence of each character in a string and then print the number of ... with detailed comments so that I can understand the logic and program? The given string is: “This is an assignment”. Thanks!
asked Jan 6 Code Learner 6.5k points