In this Program, we are going to share Reverse a Number using stack with the output. If you are a Java beginner and want to start learning the Java programming, then keep your close attention in this tutorial as I am going to share how to write reverse a number using the stack.
Java Program to reverse a number using stack
Copy the below Java program and execute it with the help of Javac compiler. At the end of this program, We have shared the output of this program.
import java.util.Stack; public class ReverseNumberProgram { static Stackst= new Stack(); static void push_digits(int number) { while(number != 0) { st.push(number % 10); number = number / 10; } } static int reverse_number(int number) { push_digits(number); int reverse = 0; int i = 1; while (!st.isEmpty()) { reverse = reverse + (st.peek() * i); st.pop(); i = i * 10; } return reverse; } public static void main(String[] args) { int number = 987654321; System.out.println(reverse_number(number)); } }
123456789
Liked this program? Do Like & share with your friends
The post Java Program to Reverse a Number Using Stack appeared first on FreeWebMentor.
This post first appeared on Programming Blog Focused On Web Technologies, please read the originial post: here