Count Number of Digits in Java
In this class, We discuss Count Number of Digits in Java.
The reader should have prior knowledge of loops in Java. Click Here.
Question:
Given an integer.
Our task is to find the number of digits in the integer.
Example:
Input: 56789
Output: 5
Logic:
Take the number 5678.
5678/10 = 567
567/10 = 56
56/10 = 5
5/10 = 0
Divide the number by ten until we get zero.
If the number has four digits, we get zero after four times
Similarly, If the number has five digits, we get zero after five times.
So we use a loop until we get zero.
Each time in the loop, we increment the count.
We take a variable count and increment.
The below diagram shows the code.
public class NoofDigits {
public static void main(String[] args) {
int i =5675;
int z=i, count=0;
while(z!=0)
{
count=count+1;
z=z/10;
}
System.out.println(count);
}
}
Link for playlists:
/ @learningmonkey
Link for our website: https://learningmonkey.in
Follow us on Facebook @ / learningmonkey
Follow us on Instagram @ / learningmonkey1
Follow us on Twitter @ / _learningmonkey
Mail us @ learningmonkey01@gmail.com
On this page of the site you can watch the video online Count Number of Digits in Java || Lesson 24 || Java Programming || Learning Monkey || with a duration of hours minute second in good quality, which was uploaded by the user Learning Monkey 21 May 2023, share the link with friends and acquaintances, this video has already been watched 492 times on youtube and it was liked by 6 viewers. Enjoy your viewing!