Count Number of Digits in Java || Lesson 24 || Java Programming || Learning Monkey ||

Publicado em: 21 Maio 2023
no canal de: Learning Monkey
492
6

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]


Nesta página do site você pode assistir ao vídeo on-line Count Number of Digits in Java || Lesson 24 || Java Programming || Learning Monkey || duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Learning Monkey 21 Maio 2023, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 492 vezes e gostou 6 espectadores. Boa visualização!