In this tutorial you will learn about the Java program to break integer into digits and its application with practical example.
In this tutorial, we will learn to create a Java program to break integer into digits using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
- User Define functions.
Steps to break number into digit.
- In our Java program to break integer into digits using a while loop.
- first of all while loop will count the total digits in a number.
- And the second while loop will iterate the Integer
- and print each digit as an output.
Java program to break integer into digits.
In this program we would find digit value from a given integer whole number.first of all we take a value from user and then break whole number into digit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Java program to find digit of given number import java.util.Scanner; public class Digitsin_no { private static Scanner sc; public static void main(String[] args) { // Decalring variables and taking values fro user int number, rem, temp, count = 0; sc = new Scanner(System.in); System.out.print("Enter Any Number = "); number = sc.nextInt(); temp = number; // copy to temp original numebr while(temp > 0) { temp = temp / 10; count++; } while(number > 0) { rem = number % 10; System.out.println(" Digit at Position " + count + " = " + rem); number = number / 10; count--; } } } |
Output
Number into Digit.
In the above program, we have first declared and initialized a set variables required in the program.
- number= it will hold entered number.
- rem= it will hold the result.
- temp= it will hold temporary number.
- count= it will count digit value.
After that we take a number from user and find given number is a Magic number or not.
Here first while loop will count the total number of digits in a given number.
And second while loop will iterate the Integer and print each digit as an output.
and Finally we will print the digit and place values as shown in image above.