write a Java Program to Count Number of Digits in an Integer

Java Program to Count Number of Digits in an Integer

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Count Number of Digits in an Integer
Java Program to Count Number of Digits in an Integer

Write a Java Program to Count Number of Digits in an Integer

PROGRAM: Java Program to Count Number of Digits in an Integer

/* Java Program to Count Number of Digits in an Integer*/

1: Count Number of Digits in an Integer using while loop

public class Main {

public static void main(String[] args) {

int count = 0, num = 0003452;

while (num != 0) {
// num = num/10
num /= 10;
++count;
}

System.out.println(“Number of digits: ” + count);
}
}

2: Count Number of Digits in an Integer using for loop

public class Main {

public static void main(String[] args) {

int count = 0, num = 123456;

for (; num != 0; num /= 10, ++count) {
}

System.out.println(“Number of digits: ” + count);
}
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *