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 @ [email protected]
Auf dieser Seite können Sie das Online-Video Count Number of Digits in Java || Lesson 24 || Java Programming || Learning Monkey || mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Learning Monkey 21 Mai 2023 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 492 Mal angesehen und es wurde von 6 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!