
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Reverse a Number
Reversing a number is a simple and fundamental question in the world of programming.
Problem Description
In this problem, we are given an integer n and we have to reverse its digit and print the reversed number. In this article, we are going to discuss different approaches to reverse a number in C#.
Example
Input
Number = 12345
Output
Reversed Number = 54321
Explanation: 54321 is the reversed form of the input number 12345 from its original order.
Input
Number = 8299
Output
Reversed Number = 9928
Explanation: 9928 is the reversed form of the input number 8299 from its original order.
Reverse a Number Using Iterative Approach
This is the most simple and common approach to reverse a number. In this approach, we extract all digits using the modulo operator until the number becomes zero. After extracting the last digit, we add it to the reverse number and remove it from the original number. After the number becomes zero, we return the reversed number.
Steps for Implementation
- Initialize a variable to zero to store the reversed number.
- Use a loop to process the number until it becomes zero.
- Extract the last digit using the modulo operator and add it to the reversed number.
- Remove the last digit of the number.
- Repeat this process until the number becomes zero.
- Return the reversed number.
Implementation Code
using System; class Program { static void Main() { Console.Write("Enter an integer: "); if (!int.TryParse(Console.ReadLine(), out int number)) { Console.WriteLine("Invalid input!"); return; } int reversedNumber = 0; while (number != 0) { int lastDigit = number % 10; if (reversedNumber > (int.MaxValue / 10) || reversedNumber < (int.MinValue / 10)) { Console.WriteLine("Error: Integer overflow while reversing."); return; } reversedNumber = (reversedNumber * 10) + lastDigit; number /= 10; } Console.WriteLine($"The Reversed Number is: {reversedNumber}"); } }
Output
Enter an integer: 12345 The Reversed Number is: 54321
Time Complexity: O(n), where n is the number of digits in the input number
Space Complexity: O(1), constant space
Reverse a Number Using Stack
A stack is a data structure that is based on the LIFO (Last In First Out) property. We can use a stack to reverse a number. We will first push all the elements onto the stack. Then, we will pop all the digits from the stack and form the reversed number.
Steps for Implementation
- Declare a stack to store digits of the number.
- Extract each digit using the modulo operator % 10 to get the last digit.
- Push the last digit onto the stack and divide the number by 10 to remove the last digit.
- Pop all the digits from the stack and construct the reversed number.
- While the stack is not empty, pop a digit, multiply the reversed number by 10, and add the popped digit.
Implementation Code
using System; using System.Collections.Generic; class Program { static int ReverseNumberUsingStack(int number) { // Handle negative numbers bool isNegative = number < 0; number = Math.Abs(number); // Stack to hold the digits of the number Stack<int> stack = new Stack<int>(); // Push each digit of the number onto the stack while (number > 0) { stack.Push(number % 10); // Get the last digit number /= 10; // Remove the last digit } // Reconstruct the number in reverse order int reversedNumber = 0; int multiplier = 1; while (stack.Count > 0) { reversedNumber += stack.Pop() * multiplier; multiplier *= 10; } // Restore the sign if the original number was negative return isNegative ? -reversedNumber : reversedNumber; } static void Main() { Console.WriteLine("Enter a number to reverse:"); int number = int.Parse(Console.ReadLine()); int reversed = ReverseNumberUsingStack(number); Console.WriteLine($"Reversed Number: {reversed}"); } }
Output:
Enter an integer: 12345 The Reversed Number is: 54321
Time Complexity: O(n)
Space Complexity: O(n), using stack