python program to reverse a number and find the sum of digits

Here is a sample Python program that reverses a number and calculates the sum of its digits:

# Function to reverse a number and find the sum of its digits
def reverse_and_sum(num):
  # Reverse the number
  reverse = 0
  while num > 0:
    reverse = (reverse * 10) + (num % 10)
    num = num // 10

  # Find the sum of the digits
  sum = 0
  while reverse > 0:
    sum += reverse % 10
    reverse = reverse // 10

  return sum

# Test the function
print(reverse_and_sum(12345))

This program defines a function reverse_and_sum() that takes an integer num as input and returns the sum of its digits after reversing it. The function first reverses the number using a while loop, and then calculates the sum of its digits using another while loop.

The function is then tested by calling it with the input 12345. The output should be 15, which is the sum of the digits 5, 4, 3, 2, and 1 (which are the digits of 12345 after it has been reversed).

Leave a Comment