Posted on by Kalkicode
Code Number

# Reverse digits of a number

The given problem is about reversing the digits of a given integer number. For example, if the input is 123, the output should be 321. Similarly, if the input is -53, the output should be -35, maintaining the negative sign.

## Problem Statement and Explanation

The task is to write a program in C that takes an integer number as input and then reverses its digits. To solve this problem, we will use a simple algorithm that involves iterating through the digits of the input number, extracting each digit, and then constructing the reversed number step by step.

## Example

Let's consider the input number as 12345. The algorithm's steps for reversing the digits will be as follows:

1. Initial state: `number = 12345`, `result = 0`.
2. First iteration: Extract the last digit of the number (`number % 10`), which is 5. Multiply the current result by 10 and add the extracted digit to it: `result = (0 * 10) + 5 = 5`. Reduce the number by removing the last digit: `number = number / 10 = 1234`.
3. Second iteration: Extract the last digit of the updated number, which is 4. Update the result: `result = (5 * 10) + 4 = 54`. Reduce the number: `number = 123`.
4. Third iteration: Extract the last digit (3) and update the result: `result = (54 * 10) + 3 = 543`. Reduce the number: `number = 12`.
5. Fourth iteration: Extract the last digit (2) and update the result: `result = (543 * 10) + 2 = 5432`. Reduce the number: `number = 1`.
6. Fifth iteration: Extract the last digit (1) and update the result: `result = (5432 * 10) + 1 = 54321`. Reduce the number: `number = 0`.

## Pseudocode

``````reverseNum(number):
result = 0
num = number
while num != 0:
last_digit = num % 10
result = (result * 10) + last_digit
num = num / 10
display "Number : " + number
display "Reverse : " + result
``````

## Algorithm Explanation

1. Initialize a variable `result` to store the reversed number and a variable `num` to store the current value of the input number.
2. Start a loop that continues until `num` becomes 0.
3. Inside the loop, extract the last digit of `num` using the modulo operator (`num % 10`) and store it in a variable `last_digit`.
4. Update the `result` by multiplying it by 10 and then adding the `last_digit`.
5. Reduce the value of `num` by dividing it by 10 (integer division).
6. Repeat steps 3-5 until `num` becomes 0.
7. Display the original number and the calculated `result` as the reversed number.

## Code Solution

Here given code implementation process.

``````// C program
// Reverse digits of a number
#include <stdio.h>

void reverseNum(int number)
{
int result = 0;
int num = number;
while (num != 0)
{
result = (result *10) + (num % 10);
num = num / 10;
}
printf("\n Number : %d", number);
// Display calculated result
printf("\n Reverse  : %d", result);
}
int main()
{
reverseNum(-53);
reverseNum(1253);
return 0;
}``````

#### input

`````` Number : -53
Reverse  : -35
Number : 1253
Reverse  : 3521``````
``````/*
Java Program for
Reverse digits of a number
*/
class Reverse
{
// Reverse digits of a given number
public void reverseNum(int number)
{
int result = 0;
int num = number;
while (num != 0)
{
// Add last digit to end of result
result = (result * 10) + (num % 10);
// Remove last digit
num = num / 10;
}
System.out.println(" Number : " + number);
// Display calculated result
System.out.println(" Reverse : " + result);
}
public static void main(String[] args)
{
// Test Case
}
}``````

#### input

`````` Number : -53
Reverse : -35
Number : 1253
Reverse : 3521``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Reverse digits of a number
*/
class Reverse
{
public:
// Reverse digits of a given number
void reverseNum(int number)
{
int result = 0;
int num = number;
while (num != 0)
{
// Add last digit to end of result
result = (result *10) + (num % 10);
// Remove last digit
num = num / 10;
}
cout << " Number : " << number << endl;
// Display calculated result
cout << " Reverse : " << result << endl;
}
};
int main()
{
// Test Case
return 0;
}``````

#### input

`````` Number : -53
Reverse : -35
Number : 1253
Reverse : 3521``````
``````// Include namespace system
using System;
/*
Csharp Program for
Reverse digits of a number
*/
public class Reverse
{
// Reverse digits of a given number
public void reverseNum(int number)
{
int result = 0;
int num = number;
while (num != 0)
{
// Add last digit to end of result
result = (result * 10) + (num % 10);
// Remove last digit
num = num / 10;
}
Console.WriteLine(" Number : " + number);
// Display calculated result
Console.WriteLine(" Reverse : " + result);
}
public static void Main(String[] args)
{
// Test Case
}
}``````

#### input

`````` Number : -53
Reverse : -35
Number : 1253
Reverse : 3521``````
``````<?php
/*
Php Program for
Reverse digits of a number
*/
class Reverse
{
// Reverse digits of a given number
public	function reverseNum(\$number)
{
\$result = 0;
\$num = \$number;
while (\$num != 0)
{
// Add last digit to end of result
\$result = (\$result * 10) + (\$num % 10);
// Remove last digit
\$num = (int)(\$num / 10);
}
echo " Number : ".\$number.
"\n";
// Display calculated result
echo " Reverse : ".\$result.
"\n";
}
}

function main()
{
// Test Case
}
main();``````

#### input

`````` Number : -53
Reverse : -35
Number : 1253
Reverse : 3521``````
``````/*
Node JS Program for
Reverse digits of a number
*/
class Reverse
{
// Reverse digits of a given number
reverseNum(number)
{
var result = 0;
var num = number;
while (num != 0)
{
// Add last digit to end of result
result = (result * 10) + (num % 10);
// Remove last digit
num = parseInt(num / 10);
}
console.log(" Number : " + number);
// Display calculated result
console.log(" Reverse : " + result);
}
}

function main()
{
// Test Case
}
main();``````

#### input

`````` Number : -53
Reverse : -35
Number : 1253
Reverse : 3521``````
``````#  Python 3 Program for
#  Reverse digits of a number
class Reverse :
#  Reverse digits of a given number
def reverseNum(self, number) :
result = 0
num = number
negative = False
if (num < 0) :
negative = True
num = -num

while (num != 0) :
#  Add last digit to end of result
result = (result * 10) + (num % 10)
#  Remove last digit
num = int(num / 10)

if (negative) :
result = -result

print(" Number : ", number)
#  Display calculated result
print(" Reverse : ", result)

def main() :
#  Test Case

if __name__ == "__main__": main()``````

#### input

`````` Number :  -53
Reverse :  -35
Number :  1253
Reverse :  3521``````
``````#  Ruby Program for
#  Reverse digits of a number
class Reverse
#  Reverse digits of a given number
def reverseNum(number)
result = 0
num = number
negative = false
if (num < 0)
negative = true
num = -num
end

while (num != 0)
#  Add last digit to end of result
result = (result * 10) + (num % 10)
#  Remove last digit
num = num / 10
end

if (negative)
result = -result
end

print(" Number : ", number, "\n")
#  Display calculated result
print(" Reverse : ", result, "\n")
end

end

def main()
#  Test Case
end

main()``````

#### input

`````` Number : -53
Reverse : -35
Number : 1253
Reverse : 3521
``````
``````/*
Scala Program for
Reverse digits of a number
*/
class Reverse()
{
// Reverse digits of a given number
def reverseNum(number: Int): Unit = {
var result: Int = 0;
var num: Int = number;
var negative: Boolean = false;
if (num < 0)
{
negative = true;
num = -num;
}
while (num != 0)
{
// Add last digit to end of result
result = (result * 10) + (num % 10);
// Remove last digit
num = (num / 10).toInt;
}
if (negative)
{
result = -result;
}
println(" Number : " + number);
// Display calculated result
println(" Reverse : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Reverse = new Reverse();
// Test Case
}
}``````

#### input

`````` Number : -53
Reverse : -35
Number : 1253
Reverse : 3521``````
``````/*
Swift 4 Program for
Reverse digits of a number
*/
class Reverse
{
// Reverse digits of a given number
func reverseNum(_ number: Int)
{
var result: Int = 0;
var num: Int = number;
var negative: Bool = false;
if (num < 0)
{
negative = true;
num = -num;
}
while (num  != 0)
{
// Add last digit to end of result
result = (result * 10) + (num % 10);
// Remove last digit
num = num / 10;
}
if (negative)
{
result = -result;
}
print(" Number : ", number);
// Display calculated result
print(" Reverse : ", result);
}
}
func main()
{
// Test Case
}
main();``````

#### input

`````` Number :  -53
Reverse :  -35
Number :  1253
Reverse :  3521``````
``````/*
Kotlin Program for
Reverse digits of a number
*/
class Reverse
{
// Reverse digits of a given number
fun reverseNum(number: Int): Unit
{
var result: Int = 0;
var num: Int = number;
var negative: Boolean = false;
if (num < 0)
{
negative = true;
num = -num;
}
while (num != 0)
{
// Add last digit to end of result
result = (result * 10) + (num % 10);
// Remove last digit
num = num / 10;
}
if (negative)
{
result = -result;
}
println(" Number : " + number);
// Display calculated result
println(" Reverse : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// Test Case
}``````

#### input

`````` Number : -53
Reverse : -35
Number : 1253
Reverse : 3521``````

## Resultant Output Explanation

In the given C program, the `main` function calls the `reverseNum` function twice with different input values: -53 and 1253.

For the first call `reverseNum(-53)`:

• The input number is -53.
• The algorithm follows the steps mentioned above.
• The result is calculated as `-35`.
• The program prints: "Number : -53" and "Reverse : -35".

For the second call `reverseNum(1253)`:

• The input number is 1253.
• The algorithm follows the same steps.
• The result is calculated as `3521`.
• The program prints: "Number : 1253" and "Reverse : 3521".

## Time Complexity

Let's analyze the time complexity of the algorithm. The number of digits in the input number `num` is proportional to the logarithm of the number in base 10. Let's denote the number of digits as `d`.

• Extracting the last digit (`num % 10`) and performing integer division (`num / 10`) both take constant time.
• The loop iterates `d` times, performing constant-time operations in each iteration.

Therefore, the overall time complexity of the algorithm is O(d), where `d` is the number of digits in the input number. In other words, the time complexity is linear in terms of the number of digits. Since `d` is typically much smaller than the value of the input number itself, the algorithm is considered efficient.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post