🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Reversing a number is a common operation in programming that can be required in various applications such as algorithms, palindromes, or simply formatting. Python provides multiple ways to reverse a number, and in this blog post, we'll focus on a simple and efficient method to achieve this.
Reversing a number means reordering its digits in the opposite direction. For example, if we reverse the number 12345, the output should be 54321. This process involves taking each digit from the original number, starting from the end, and appending it to a new number.
2. Program Steps
1. Accept or define the number to be reversed.
2. Initialize a variable to store the reversed number.
3. Extract each digit from the original number and add it to the reversed number.
4. Update the original number by removing the extracted digit.
5. Repeat steps 3 and 4 until the original number is reduced to zero.
6. Output the reversed number.
3. Code Program
# Function to reverse a number
def reverse_number(n):
# Initialize the reversed number to 0
reversed_num = 0
# Loop until the number is greater than 0
while n > 0:
# Get the last digit of the number
digit = n % 10
# Add the digit to the reversed number's correct place
reversed_num = reversed_num * 10 + digit
# Remove the last digit from the number
n = n // 10
# Return the reversed number
return reversed_num
# Number to be reversed
number = 12345
# Call the function and print the result
print(reverse_number(number))
Output:
54321
Explanation:
1. The function reverse_number is defined to take an integer n.
2. reversed_num is initialized to 0 and will be used to construct the reversed number.
3. A while loop continues to run as long as n is greater than 0.
4. Inside the loop, digit extracts the last digit of n using the modulus operator n % 10.
5. reversed_num is updated by multiplying it by 10 (to shift digits to the left) and adding digit to it.
6. n is updated by performing integer division by 10, effectively removing the last digit from n.
7. Once n is reduced to 0, the loop ends, and reversed_num is returned.
8. The function is then called with number as the argument, and the result is printed, showing 54321, the reverse of 12345.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment