Question
when i run my code it get the wrong answer for the final operation. if user enters number it does not show the odd numbers
when i run my code it get the wrong answer for the final operation. if user enters number it does not show the odd numbers added up. I want user to input number such as 3316922 but I was to add up the odd numbers (3+3+1+9=16) However if the user enters 12345 as the number, it calculates it correctly. 1+3+5 = 9! If I enter 123456 it counts to 12 which is incorrect and should still be 9 if i enter 338912 it gives 14 but 3+3+9+1 is 16
Below is code and bolded section at bottom is the operation with the problem it is showing odd places and not odd digits.. how would you correct this?
i = 2 sum = 0 print("Even numbers between 2 and 200 : ") while(i <= 200): if(i % 2 == 0): print(i, end = ' ') sum = sum + i i = i + 1 print(" Sum of even numbers between 2 and 200 :", sum)
print(" Square of number between 1 to 100: ") i = 1 sum = 0 while(i <= 100): sum = sum + (i * i) print((i*i), end =' ') i = i + 1 print(" Sum of square of number from 1 to 100 : ", sum)
i = 1 print(" Powers of 2 from 1 to 256 :") while(i <= 16): print(" ", i ** 2) i = i + 1
a = int(input("Enter the lower limit :")) b = int(input("Enter the upper limit :")) sum = 0 print(" Odd numbers between ", a,"and ", b," : ") x = a while(a <= b): if(a % 2 == 1): print(a, end = ' ') sum = sum + a a = a+1 # given a + 1 print(" Sum of odd numbers between ", x, "and", b, ":",sum)
num = int(input("Enter a number :")) sum = 0 i = 1 while(num != 0): a = num % 10 if(i % 2 == 1): sum = sum + a #Given sum = sum + x num = num // 10 i = i + 1 print(" Sum of odd digits :", sum) input("Exit?")
***sum of all odd digits, not places.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started