Program to reverse a number
In this post, you will learn how to write program in different languages to reverse a number. Such a type of question can improve your logical problem solving skills.
C program to reverse a number using recursion
A recursion function is a function that is called itself. In the given example, we call the recursion function to get the reverse of a number. A recursion function continues until some condition is met to prevent it. That's why we use the if statement to break the infinite recursion. In the given C program, we have used recursion to reverse a given number-
#include <stdio.h>
long reverse(long);
int main()
{
long num, rev;
printf("Please enter an integer : ");
scanf("%ld", &num);
rev = reverse(num);
printf("Reverse of entered number : %ld\n", rev);
return 0;
}
long reverse(long num) {
static long rev = 0;
if(num == 0)
return 0;
rev = rev * 10;
rev = rev + num % 10;
reverse(num/10);
return rev;
}
Javascript program to reverse a number using in-built methods
Here, we have used toString(), split(), reverse() and join() methods to reverse a number. The toString() converts a given number into a string. The split() method divides a string into an ordered list of substrings, places them in an array, and returns the array. The reverse() method changes the sequence of elements of the given array and returns the reverse sequence and the join() method returns the array as a string. The Math.sign(num) multiplies the number with the sign of the original number provided, and the parseFloat(num) converts num into a float from a string.
// program to reverse a number
function reversedNumber(num) {
return (
parseFloat(
num
.toString()
.split('')
.reverse()
.join('')
) * Math.sign(num)
)
}
num = 34122332;
document.write("Original number: "+num+"<br/>");
result = reversedNumber(num);
document.write("Reverse number: "+result+"<br/>");
Output of the above code:
Original number: 34122332
Reverse number: 23322143
C++ program to reverse a number
This given C++ program takes an integer input from the user and stores it in a variable num.
#include <iostream>
using namespace std;
int main() {
// Initializing variables
int num, reversedNumber = 0, remainder;
// User input
cout << "Enter an integer: ";
cin >> num;
while(num != 0) {
remainder = num%10;
reversedNumber = reversedNumber*10 + remainder;
num /= 10;
}
cout << "Reversed Number = " << reversedNumber;
return 0;
}
Output of the above code:
Enter an integer: 98345673
Reversed Number = 37654389
Java program to reverse a number using While Loop
In the given Java program, we are taking the input number from the user using the Scanner class and then we are reversing the number using while loop.
import java.util.Scanner;
public class ReverseNumberProgram
{
public static void main(String[] args)
{
// taking user input
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number : ");
int num =sc.nextInt();
int rev = 0;
while(num != 0)
{
int remainder = num % 10;
rev = rev * 10 + remainder;
num = num/10;
}
System.out.println("Reverse of the given number : " + rev);
}
}
Output of the above code:
Enter a number : 349871
Reverse of the given number : 178943
Python program to reverse a number using while loop
In the given Python program, we have asked the user to enter an integer number. The while loop is used, and the last digit of the number is obtained by using the modulus operator. The last digit is then stored at the one position, the second last at the ten position, and so on. The last digit is then removed by truly dividing the number with 10. This loop terminates when the value of the number is 0. Then, the reverse of the number is printed.
# user input
num = int(input("Enter the number: "))
# Initiate value to null
rev_num = 0
# reverse the integer number using the while loop
while (num > 0):
remainder = num % 10
rev_num = (rev_num * 10) + remainder
num = num // 10
# Display the result
print("The reverse number : {}".format(rev_num))
Output of the above code:
Enter the number: 98235781
The reverse number : 18753289
Related Articles
Java program to find area of triangleArea of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to convert Decimal to Octal
C program to convert decimal to binary
C program to check whether a number is even or odd