Write a code to reverse a number in Java, C, C++ and Python
Introduction:
One of the classic programming challenges is reversing a number. This problem not only tests a coder's fundamental understanding but also their ability to think algorithmically across different programming languages. Below, we'll explore the solutions in C, C++, Java, and Python, but first, let's visualize what reversing a number means.
Visual Representation:
Imagine the number is 7789. Reversing it would yield 9877. Let's break this process down to make it more digestible:
- Start with 7789
- Extract the last digit (9) and add it to the reverse
- Remove the last digit, leaving 778
- Repeat until no digits are left
- Result: 9877
Solutions:
Now let us see the solution in different programming languages.
1. Java
1. Java
public class ReverseNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
// Reading the number from the user
int n = scanner.nextInt();
int reverse = 0;
// Loop until all digits are processed
while(n != 0) {
// Extract the last digit and add it to the reversed number
reverse = reverse * 10 + n % 10;
// Remove the last digit from the original number
n /= 10;
}
// Print the reversed number
System.out.println("Reversed number = " + reverse);
}
}
2. Python
# Reading the number from the user
n = int(input("Enter a number: "))
reverse = 0
# Loop until all digits are processed
while n != 0:
# Extract the last digit and add it to the reversed number
reverse = reverse * 10 + n % 10
# Remove the last digit from the original number
n //= 10
# Print the reversed number
print("Reversed number =", reverse)
3. C++
#include <iostream>
using namespace std;
int main() {
int n, reverse = 0;
cout << "Enter a number: ";
// Reading the number from the user
cin >> n;
// Loop until all digits are processed
while(n != 0) {
// Extract the last digit and add it to the reversed number
reverse = reverse * 10 + n % 10;
// Remove the last digit from the original number
n /= 10;
}
// Print the reversed number
cout << "Reversed number = " << reverse;
return 0;
}
4. C
int main() {
int n, reverse = 0;
printf("Enter a number: ");
// Reading the number from the user
scanf("%d", &n);
// Loop until all digits are processed
while(n != 0) {
// Extract the last digit and add it to the reversed number
reverse = reverse * 10 + n % 10;
// Remove the last digit from the original number
n /= 10;
}
// Print the reversed number
printf("Reversed number = %d", reverse);
return 0;
}
Conclusion:
Reversing a number might seem straightforward, but it encapsulates key programming concepts such as loops and arithmetic operations. Regardless of the language, the approach remains the same: isolate digits, reconstruct in reverse order, and handle edge cases gracefully.
** Please do subscribe my blog for future updates and share with your friend, if you find this informative **
These are all toy solutions, don't use them in your real code. Just convert to string, reverse the string, and convert back for a robust and readable way, suitable for the vast majority of cases
ReplyDelete