This is a Java Program to Extract Digits from A Given Integer.
Enter any integer as input. After that we perform several operations like modulus and division to know the number of digits in given integer and then print each of the digits of the given number.
Here is the source code of the Java Program to Extract Digits from A Given Integer. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Extract_Digits
{
public static void main(String args[])
{
int n, m, a, i = 1, counter = 0;
Scanner s=new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
m = n;
while(n > 0)
{
n = n / 10;
counter++;
}
while(m > 0)
{
a = m % 10;
System.out.println("Digits at position "+counter+":"+a);
m = m / 10;
counter--;
}
}
}
Output:
$ javac Extract_Digits.java $ java Extract_Digits Enter any number:5678 Digits at position 4:8 Digits at position 3:7 Digits at position 2:6 Digits at position 1:5
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Check Java Books
- Apply for Java Internship
- Practice BCA MCQs
- Check Programming Books
- Apply for Computer Science Internship