REVERSE THE GIVEN NUMBER

REVERSE THE GIVEN NUMBER

INTRODUCTION

Write a program to REVERSE THE GIVEN NUMBER.

Ex1:
Input: 12345
Output: Reversed number is: 54321.

Ex2:
Input: 9876
Output: Reversed number is: 6789.

APPROACH:

The idea behind reversing a given number is to extract the last digit of the number and form a new number in the reverse order.

To extract the last digit, we will simply divide the number by 10, the remainder which results out is the last digit of the number. So by applying relevant formulas, we will reverse the given number efficiently.

To reverse a number, we will extract the last digit by using the ‘%’ operator. After extracting the last digit. We will add to a temporary variable, let’s take it as a sum, by multiplying by 10. this iteration will be continued till the number is greater than zero.

ALGORITHM:

Step 1: Start.
Step 2: Read an input number. 
Step 3: Extract the last digit of the number by using the ‘%’ operator.
Step 4: Add to a temporary variable, let’s take it as sum, by using the formulae sum=sum*10+last_digit.
Step 5: Return the value which is stored in the sum variable.
Step 6: Stop.

C++ CODE:

#include <iostream>
using namespace std;

int main() {
     int n;
    cin>>n;
    int temp = n,sum=0,digit=0;
    while(n>0){
      digit = n%10; 
      sum=sum*10+digit;
      n = n/10;
    }
cout<<"Reversed number is : "<<sum;
}

JAVA CODE:

import java.util.*;
public class Main {
    public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      int n = s.nextInt();
      int sum=0,digit=0;

      while(n>0){
        digit = n%10;
        sum=sum*10+digit;
        n = n/10;
      }

      System.out.println("Reversed number is : "+sum);
    }
}

PYTHON CODE:

n = int(input())
temp = n
digit = 0
sum = 0
while n>0:
  digit = n%10
  sum=sum*10+digit;
  n = n//10
print("Reversed number is : ",sum))


Input1: 12345
Output1: Reversed number is: 54321

Input2: 9876
Output2: Reversed number is: 6789

The time complexity of the code is O(LogN).
The space complexity of the code is O(1).

USING RECURSION

We can reverse the given number by using recursion. It would follow the following steps.

See also  Heap sort algorithm

In this approach, instead of iterating multiple times, we start using recursion. We call the same function with n/ until n becomes less than 10.

C++ CODE:

#include <iostream>
using namespace std;

int reverse(int n){
  if(n>10){
  cout<<n%10;
  reverse(n/10);
  }
}

int main() {
  int n;
  cin>>n;
  cout<<"Reversed number is : "<<reverse(n);
}

JAVA CODE:

import java.util.*;
public class Main {
    public static void reverseNumber(int number)   {  
    if (number < 10) 
       System.out.println(number);  
    else  {  
        System.out.print(number % 10);  
        reverseNumber(number/10); 
    }  
}
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);  
    int num = s.nextInt();  
    System.out.print("Reversed number is : ");  
    reverseNumber(num); 
  }
}
Input1: 12345
Output1: Reversed number is: 54321

Input2: 9876
Output2: Reversed number is: 6789

Here is an example of a function that reverses a number using recursion in Python:

Python Code:

def reverse_number(num):
    if num == 0:
        return num
    else:
        return int(str(num % 10) + str(reverse_number(num // 10)))

In this example, the function first checks if the number is 0, and if so, it returns 0. If not, it returns the last digit of the number concatenated with the result of the function being called again with the number divided by 10. This continues until the number is reduced to 0, at which point the concatenation of all of the digits in reverse order will have been returned.

String conversion

There is a unique method in python that helps us to reverse the number directly. So, we can reverse the number by using the above method. This method is known as reversing. Let’s see the code.

See also  12 simple coding projects and best Programming Ideas for beginners

PYTHON CODE:

n = str(int(input()))
print("Reversed number is :",n[::-1])
Input1: 12345
Output1: Reversed number is: 54321

Input2: 9876
Output2: Reversed number is: 6789

In the above program, we have used the reversing method to reverse the given string which we had taken as input.

CONCLUSION

That’s it from this tutorial. Hope you guys found It interesting. We have solved the reversing of the number in different programming languages by using different numbers. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.