Java Program to Extract Digits From a Given Integer

Java program to extract digits from a given integer. This is a java program for extracting digits from an integer value. As an input, you can use any integer. After that, we perform operations such as modulus and division to determine the number of digits in a given integer, and then we print each of the digits. 

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int n, m, a, i = 1, checker = 0;
Scanner s=new Scanner(System.in);
System.out.print("Enter any numbers");
n = s.nextInt();
m = n;
while(n > 0)
{
n = n / 10;
checker++;
}
while(m > 0)
{
a = m % 10;
System.out.println("numbers position is "+checker+":"+a);
m = m / 10;
checker--;
}
}
}

 

Output:

Enter any numbers  4567
numbers position is 4:7
numbers position is 3:6
numbers position is 2:5
numbers position is 1:4

Pre Increment and Post Increment in Java

Before assigning a value to a variable, the pre increment operator is used to increase its value by 1. After assigning a value to a variable, the post increment operator is used to increase its value by one.

While Loop in Java Program

The condition is assessed first in a while loop, and if it returns true, the statements inside the while loop are executed. This process is repeated until the condition returns false. When the condition returns false, the control exits the while loop and moves on to the next sentence in the program.