Program to Print Sum of all Odd Digits present in a Number in Python

Python Program to Print Sum of all Odd Digits present in a Number

Sum of all Odd Digits present in a Number

Today, we’ll write a program to Print Sum of all Odd Digits present in a Number and the Sum of all odd digits in a given number can be found by separating each digit from the number and checking whether the digit is odd or not if odd then add that digit if even ignore that. We can also find this by taking input as a string, and reading the algorithm to understand. 

  • Sample input: 8734835
  • Sample output: 18
  • Explanation: odd number in a given number is 7,3,3,5 and the sum is 18
Program to Print Sum of all Odd Digits present in a Number in Python

Working :

  • Step 1: Read the integer input
  • Step 2: initialize s and set it to zero
  • Step 3: using modulo with 10 logic we will get last digit of the number 
  • Step 4: Do this operation by using while loop
  • Step 5: Check whether the number is odd or not , if number is odd add to the result
  • Step 6: Print the resultant sum

Python program to Print Sum of all Odd Digits present in a Number:

Run
n=int(input("Enter the number : "))
s=0
while n>0:
    n1=n%10
    if(n1%2==1):
        s=s+n1
    n=n//10
print("Sum of odd numbers in given number :",s)
Enter the number : 8734835
Sum of odd numbers in given number : 18