In this program, you’ll learn to count the number of digits using a while loop and for loop in Java.
To understand this example, you should have the knowledge of the following Java programming topics:
public class Countofdigit
{
public static void main(String[] args)
{
int box = 6012374;
int total = 0;
int count = 0;
while(box>0) //6012374>0 601237>0 60123>0
{
System.out.println("Hi"); // hii hii hii
int rem = box%10; // 4 7 6
total = total + rem; // 0=0+4=4 4=4+7=11 23
box=box/10; //6012374/10=601237
//601237/10=60123 0
count++;// 1 2 7
}
System.out.println("Count of Digits " + count);// 7
System.out.println("Sum of Digits " + total);
}
}
output:1

output:2
