top of page

Reverse a Number

  • Shreyas Naphad
  • May 8, 2024
  • 1 min read

Updated: Jun 5, 2024

In this article, we will be solving a very commonly asked problem which is to reverse a number.

Problem Statement:

Given an integer N, write a program to reverse N.

Example:

N: 245

Output: 542

The C++ Code to solve this problem is as follows:

 

Explanation:

·        We store the integer 245 in a variable named number and create a variable rev to reverse this number and give a starting value of 0.

·        While the number is not equal to 0, we will extract the last digit and append it in rev. After this, we eliminate that last digit from our original number.

·        We repeat this until the number becomes 0 and rev will eventually be the reverse of the original number.

 

Time Complexity: O(n)

Space Complexity: O(1)

Comments


©2025 by DevSparks.

bottom of page