Reverse a Number in Python with a While Loop

Hello connections 👋 Welcome to Day 4 of my Python problem-solving series! Small steps every day lead to big growth 🚀 🧠 Day 4 Challenge: Reverse a Number Write a Python program to reverse a given number. 👉 Example: Input: 1234 → Output: 4321 Input: 560 → Output: 65 My Approach : Using While Loop num = int(input("Enter a number: ")) temp = num rev = 0 while temp > 0: r = temp % 10 rev = (rev * 10) + r temp =temp// 10 print("Reversed Number =", rev) 📌 Explanation: We extract the last digit using % 10, add it to the reversed number, and remove the last digit using // 10. Now it’s your turn 👇 Try solving it with your own method or suggest a better approach in the comments. Let’s learn and grow together 🚀 #Python #CodingChallenge #ProblemSolving #Programming #30DaysOfCode

To view or add a comment, sign in

Explore content categories