WAP to enter a number and find the sum of odd digits.
eg:2345 sum of odd digits= 3+5=8
Answers
Answered by
8
The following codes have been written using Python.
Once the data is input, we create a list. We append each digit of the input into the list using an iteration statement/loop. Now that each digit is present in the list, we start another loop to traverse through the list's elements and check if it's an odd/even number using a conditional statement. If it is an odd digit, we add it to 's', which will give us the final sum in the print statement.
While appending each digit into the list, we use the range as str(n), as the digits of a number cannot be traversed through. Since the digits will be in their string form in the list, we use int(i) for the loop to perform calculations, i.e., checking if it's an odd/even digit.
Similar questions