Part of the series

Several example codes

~2 min read • Updated Oct 4, 2025

Program Overview

This Python program reads a number from the user and processes it digit by digit from right to left.
For each digit, it checks if the digit is odd. If so, it multiplies the digit by its position (starting from 1 on the right).
The program displays a table showing the number at each step, the extracted digit, and the product (if odd).
Finally, it prints the total sum of all such products.


Python Code:


# Read number from user
num = int(input("Enter a number: "))
position = 1
total = 0

print("Number\tDigit\tMultiply")

while num > 0:
    digit = num % 10
    if digit % 2 == 1:
        product = digit * position
        total += product
    else:
        product = 0

    print(f"{num}\t{digit}\t{product}")
    num //= 10
    position += 1

print("Total weighted sum of odd digits:", total)

Sample Output (for input 3276):


Number  Digit   Multiply  
3276    6   0  
327 7   14  
32  2   0  
3   3   1  
Total weighted sum of odd digits: 15

Step-by-Step Explanation:

- num % 10 extracts the last digit
- digit % 2 == 1 checks if the digit is odd
- If odd, digit * position is added to the total
- num //= 10 removes the last digit for the next iteration
- position increases with each step, starting from 1


Written & researched by Dr. Shahin Siami