Table of Contents
Algorithm
algorithm to reverse a number can be described using the following steps:
- Initialize a variable to store the reversed number, let’s call it
reversedNumber
, and set it to 0. - Extract the rightmost digit of the given number by using the modulo operator
%
with 10. Let’s call this digitremainder
. - Multiply the current
reversedNumber
by 10 and add theremainder
to it. This step appends theremainder
digit to the reversed number. - Divide the given number by 10, discarding the rightmost digit. This step removes the rightmost digit from the given number.
- Repeat steps 2 to 4 until the given number becomes 0.
- The final value of
reversedNumber
will be the reversed form of the original number.
C
#include <stdio.h> int reverseNumber(int number) { int reversedNumber = 0; while (number != 0) { int remainder = number % 10; reversedNumber = reversedNumber * 10 + remainder; number /= 10; } return reversedNumber; } int main() { int number; printf("Enter a number: "); scanf("%d", &number); int reversedNumber = reverseNumber(number); printf("Reversed number: %d\n", reversedNumber); return 0; }
the reverseNumber()
function takes an integer as input and reverses it. It uses a while loop to extract the digits of the number one by one and builds the reversed number by multiplying the current reversed number by 10 and adding the extracted digit. Finally, the reversed number is returned.
In the main()
function, the user is prompted to enter a number. The scanf()
function is used to read the input and store it in the number
variable. Then, the reverseNumber()
function is called, passing the number
as an argument. The reversed number is stored in the reversedNumber
variable and printed using printf()
.
C++
#include <iostream> int reverseNumber(int number) { int reversedNumber = 0; while (number != 0) { int remainder = number % 10; reversedNumber = reversedNumber * 10 + remainder; number /= 10; } return reversedNumber; } int main() { int number; std::cout << "Enter a number: "; std::cin >> number; int reversedNumber = reverseNumber(number); std::cout << "Reversed number: " << reversedNumber << std::endl; return 0; }
we use similar logic as the C program to reverse the number. The reverseNumber()
function takes an integer as input, reverses it, and returns the reversed number.
In the main()
function, the user is prompted to enter a number using std::cout
. The input is then read using std::cin
and stored in the number
variable. The reverseNumber()
function is called, passing the number
as an argument. The reversed number is stored in the reversedNumber
variable.
Finally, the reversed number is printed to the console using std::cout
along with std::endl
.
java
import java.util.Scanner; public class ReverseNumber { public static int reverseNumber(int number) { int reversedNumber = 0; while (number != 0) { int remainder = number % 10; reversedNumber = reversedNumber * 10 + remainder; number /= 10; } return reversedNumber; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int number = scanner.nextInt(); int reversedNumber = reverseNumber(number); System.out.println("Reversed number: " + reversedNumber); } }
we define a class called ReverseNumber
. It has a reverseNumber()
method that takes an integer as input, reverses it, and returns the reversed number.
In the main()
method, we create a Scanner
object to read user input. The user is prompted to enter a number using System.out.print()
, and the input is read using scanner.nextInt()
and stored in the number
variable.
Then, we call the reverseNumber()
method, passing the number
as an argument. The reversed number is stored in the reversedNumber
variable.
Finally, we print the reversed number to the console using System.out.println()
.
Python
def reverse_number(number): reversed_number = 0 while number != 0: remainder = number % 10 reversed_number = reversed_number * 10 + remainder number //= 10 return reversed_number number = int(input("Enter a number: ")) reversed_number = reverse_number(number) print("Reversed number:", reversed_number)
we define a function called reverse_number()
that takes an integer as input, reverses it, and returns the reversed number.
We use a while
loop to extract the digits of the number one by one. Inside the loop, we calculate the remainder by using the modulo operator %
with 10. We then update the reversed_number
variable by multiplying it by 10 and adding the remainder. Finally, we update the number
by using integer division //
with 10 to remove the last digit.
In the main part of the code, we prompt the user to enter a number using the input()
function and convert it to an integer using int()
. We then call the reverse_number()
function, passing the number
as an argument. The reversed number is stored in the reversed_number
variable.