- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to Count the number of digits in a given integer
Read a number from user. Create an integer (count) initialize it with 0. Divide the number with 10. till the number is 0 and for each turn increment the count.
Example
import java.util.Scanner; public class CountingDigitsInInteger { public static void main(String args[]){ Scanner sc = new Scanner(System.in); int count = 0; System.out.println("Enter a number ::"); int num = sc.nextInt(); while(num!=0){ num = num/10; count++; } System.out.println("Number of digits in the entered integer are :: "+count); } }
Output
Enter a number :: 1254566 Number of digits in the entered integer are :: 7
Advertisements