Program to reverse a number in C, C++, Java and Python | faceprep

Program to reverse a number is discussed here. We can reverse a number using loop and arithmetic operators in both iterative and recursive approaches. 

For example,

Input: 13579
Output: 97531

Algorithm to reverse a number

  • Input the number from the user.
  • Perform rem = number % 10 and print rem.
  • Perform number = number / 10.
  • Repeat steps 2 and 3 until the number cannot be divided any further.

Iterative program to reverse a number

// C program to reverse a number

#include <stdio.h>
int main()
{
int n, rev = 0, rem;
printf(“\nEnter a number : “);
scanf(“%d”, &n);
printf(“\nReversed Number : “);
while(n != 0)
{
rem = n%10;
rev = rev*10 + rem;
n /= 10;
}

printf(“%d\n”, rev);

return 0;
}

// C++ program to reverse a number

#include <iostream>
using namespace std;

int main()
{
int n, rev = 0, rem;
cout << “\nEnter a number : “;
cin >> n;
cout << “\nReversed Number : “;
while(n != 0)
{
rem = n%10;
rev = rev*10 + rem;
n /= 10;
}

cout << rev << endl;

return 0;
}

// Java program to reverse a number

import java.util.*;
public class Main
{

public static void main(String[] args)
{
int n, rem = 0, rev = 0;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter a number : “);
n = sc.nextInt();
while(n != 0)
{
rem = n%10;
rev = rev*10 + rem;
n /= 10;
}
System.out.println(“Reversed Number : ” + rev);


}
}

# Python program to reverse a number

print(“Enter a number : “)
n = int(input())
rev = 0
rem = 0
while(n != 0):
rem = n%10
rev = rev*10 + rem
n = int(n/10)

print(“Reversed Number : “,rev)

 

Time complexity: O(log(n))

Recursive program to reverse a number

// C program to reverse a number using recursion

#include

/* Recursive approach to reverse digits of num*/
int reverse_digits(int num)
{
static int rev_num = 0;
static int base_pos = 1;
if(num > 0)
{
reverse_digits(num/10);
rev_num += (num%10)*base_pos;
base_pos *= 10;
}
return rev_num;
}

int main()
{
int num;
printf(“\nEnter a number : “);
scanf(“%d”,&num);
printf(“Reversed number : %d “,reverse_digits(num));

return 0;
}

// C++ program to reverse a number using recursion

#include <bits/stdc++.h>
using namespace std;

/* Recursive approach to reverse digits of num*/
int reverse_digits(int num)
{
static int rev_num = 0;
static int base_pos = 1;
if(num > 0)
{
reverse_digits(num/10);
rev_num += (num%10)*base_pos;
base_pos *= 10;
}
return rev_num;
}

int main()
{
int num;
cout << “\nEnter a number : “;
cin >> num;
cout << “Reversed number : ” << reverse_digits(num);

return 0;
}

// Java program to reverse a number using recursion

class Main
{
static int rev_num = 0;
static int base_pos = 1;

// Recursive approach to reverse digits of num
static int reverse_digits(int num)
{
if(num > 0)
{
reverse_digits(num / 10);
rev_num += (num % 10) * base_pos;
base_pos *= 10;
}
return rev_num;
}

public static void main(String[] args)
{
int num = 1234;
System.out.println(reverse_digits(num));
}
}

# Python program to reverse a number using recursion

rev_num = 0
base = 1

# Recursive function to reverse a number
def reverse_digits(num):
global rev_num
global base
if(num > 0):
reverse_digits((int)(num / 10))
rev_num += (num % 10) * base
base *= 10
return rev_num

num = 1907
print(“Reversed number is “, reverse_digits(num))

Output:

reverse a number

Time complexity: O(log(n))