How to break a number to individual digit

0 votes

I know this is very basic and the number reversal code for java is also similar but the problem I am facing is, I am not understanding how to simply break the number to digit. Please help

Input:

1234

Output:

1
2
3
4

Mar 6, 2019 in Java by Bobin
310 views

1 answer to this question.

0 votes

The best way would be find the length of the number,

public class Break
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int n= sc.nextInt();
        int res,rem,num=0;
        while(n!=0)
        {
            res = n/10;
            rem = n % 10;
            System.out.println(rem);
            n = res;
        }    
    }
}

answered Mar 7, 2019 by Priyaj
• 58,090 points

Related Questions In Java

0 votes
1 answer

How to count the number of occurrences of an element in a List?

We can use the static frequency() method. int ...READ MORE

answered Aug 21, 2018 in Java by sharth
• 3,370 points
5,319 views
+1 vote
1 answer

How to print factorial of a number using While loop?

While also works like for just you ...READ MORE

answered Nov 22, 2018 in Java by Namitha
1,128 views
0 votes
1 answer

How to round a number to n decimal places in Java?

Use setRoundingMode, set the RoundingMode explicitly to handle your issue ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,910 points

edited Jul 6, 2023 by Khan Sarfaraz 1,392 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,572 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,035 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
981 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,377 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,177 views
+4 votes
1 answer

How to print the individual occurance of elements in Java?

You can use a HashMap to serve ...READ MORE

answered Dec 4, 2018 in Java by Priyaj
• 58,090 points
904 views
0 votes
2 answers

How to iterate in a HashMap in Java?

Iterating using Iterator. Using Generics: Map<Integer, Integer> map = ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
759 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP