Computer Science, asked by sagarkumar4k9, 24 days ago

WAP to enter a number and find the sum of odd digits.

eg:2345 sum of odd digits= 3+5=8​

Answers

Answered by Equestriadash
8

The following co‎des have been written using Python.

\tt n\ =\ int(in put("Enter\ a\ number: "))\\l\ =\ list()\\for\ i\ in\ str(n):\\{\ \ \ \ \ }l.append(i)\\s\ =\ 0\\for\ i\ in\ l:\\{\ \ \ \ \ }if\ int(i)\%2\ !=\ 0:\\{\ \ \ \ \ }{\ \ \ \ \ }s\ =\ s\ +\ int(i)\\print(s)

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