Write a java program to take a number and return list of its digits

Write a java program to take a number and return list of its digits. Take a number and return a list of its digits with a Java program. Take a number and return a list of its digits with a Java program.

As an input, you can use any integer number. The amount of digits in the given input is then counted. The given number, along with its length, is handed to the other function, which returns all of the digits of the given number via modulus and division operations.

import java.util.Scanner;
public class Main
{
int[] digits(int m,int l)
{
int x[] = new int[l];
int i = 0;
while(m > 0)
{
x[i] = m % 10;
m = m / 10;
i++;
}
return x;
}
public static void main(String[] args)
{
int counter = 0, n, m;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
m = n;
while(n > 0)
{
n =n / 10;
counter++;
}
Main obj=new Main();
int a[]=obj.digits(m, counter);
System.out.print("Digits:");
for(int i = a.length-1; i >= 0; i--)
{
System.out.print(a[i]+" ");
}
}
}

 

Output:

Enter any number: 45
Digits:4 5

What is while loop in java program and how it works

The while loop in Java is a control flow statement that allows code to be repeated based on a Boolean condition. The while loop is similar to a looping if statement. If the condition evaluates to true, the body of the loop will be executed, and we will proceed to the update expression.

Purpose of modulus operator in java program

The remainder of a division of one integer by another is returned by the modulus operator. Modulo is usually denoted by a percent sign in most programming languages. Because 2 divides into 4 precisely without a leftover, “4 mod 2” or “4 percent 2” returns 0

Post increament operator in java programming

After utilizing  a variable in an expression, the post increment operator is used to increase its value. The value is used inside the expression and then incremented by one in the post increment.