Java program to reverse a given number

Write a java program to reverse a given number by using only numeric operators.Suppose, if you are given an input of 4567 then the output should be 7654.

In the program below, we have only used the modulus or remainder and / operator.

Kotlin program to reverse a given number


fun reverseNumber(number: Int): Int {
    var number = number
    var reverse = 0
    while (number != 0) {
        reverse = reverse * 10 + number % 10
        number = number / 10
    }
    return reverse
}

fun main(a: Array<String>) {
    var num = 3579
    println("Original Number : " + num)
    println("Reversed Number : " + reverseNumber(num))
}


Output


Original Number : 3579
Reversed Number : 9753


By using modulus operator(%) on the input Int by 10 will extract off the rightmost digit.
Example: (1234 % 10) = 4.
Multiplying an integer by 10 will push it left exposing a zero to the right of that number
Example: (5 * 10) = 50.
Dividing an integer by 10 will remove the rightmost digit. (75 / 10) = 7

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions