How can I separate the digits of an int number in Java?

Java

If I have a number 2500, I want the output to be: 2, 5, 0, 0. How can I separate the numbers into digits?

1
Answers

Replies

he process to separate the digits of an integer number using Java is simple. Below is the code that is used to separate the digits of an integer number. 


public static void main(String[] args) 


{



int n=1000; // int number



while (n > 0) {



    System.out.println( n % 10);



    n = n / 10;



}



}

 
 

If you want to unleash your potential in this competitive field, please visit the Java course page for more information, where you can find the Java tutorials and Java frequently asked interview questions and answers as well.

 

This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.