Write a program that reads n digit number. after reading the number, compute and display the sum of odd positioned digits,multiply all even positioned digits and add the two numbers. I have attempteed this problem like this

``````a = input("Enter number")
s = 0
p = 1
n = 1
while a>0:
digit=a%10
if n%2 == 0:
p = p*digit
else:
s = s+digit a=a/10
n=n+1

print "The sum is",s
print "Product is",p
``````

it works perfectly for even no of digits but for odd no of digits like for 234 it shows the sum as 6 and product 3 whereas it should have been sum=3 and product 8. i am sure there is something wrong with my n value. pls help

5
Contributors
8
Replies
55
Views
4 Years
Discussion Span
Last Post by sneekula

The odd numbers in your example are 2 and 4. So 6 is correct.
The even number in your example is 3. So 3 is correct.
I would've probably did something like this:

``````def findproductandsum(userinput):
# Initialize counters.
finalproduct, finalsum = 1, 0

# Iterate over index and character in userinput.
for i, digitchar in enumerate(userinput):
# Convert digit character to integer
digit = int(digitchar)
# Position is i + 1 since the index is zero-based.
if (i + 1) % 2 == 0:
# Even.
finalproduct *= digit
else:
# Odd.
finalsum += digit
return finalproduct, finalsum

# Use 'raw_input' on Python 2 though, it's a good habit to get into.
n = input('Enter a number: ')

# findproductandsum returns a tuple, so unpack it into p and s.
p, s = findproductandsum(n)
print('n: {}, product: {}, sum: {}'.format(n, p, s))
``````

You can check it using only evens (product):

`````` assert(findproductandsum('01010101') == (1, 0))
``````

and only odds (sum):

`````` assert(findproductandsum('10101010') == (0, 4))
``````

Edited by chriswelborn: Formatting

i havnt done so much of python yet. cant we do it without defining a function. also my teacher tells me that the first digit is 0th digit then so on so in 234:2 and 4 are even 3 is odd so sum is 3 and product 8. please write down a simpler code.

If you print "digit" you will see that it goes from right to left so for input that is an even number in length, the code is bass ackwards. Also a=a/10 only executes under the else. Use a for loop instead of a while loop with your logic or something like the following.

``````orig_number = "234756"

## sum of odd position
total_sum = 0
for ctr in range(0, len(orig_number), 2):
print orig_number[ctr],
total_sum += int(orig_number[ctr])
print "\n2+4+6 =", total_sum

## multiply even positions
total_multiply = 1
for ctr in range(1, len(orig_number), 2):
total_multiply *= int(orig_number[ctr])
print "3*5*7", total_multiply
``````

Edited by woooee

@woooee, your print statements are off.

Output:

``````2+4+6 = 11
3*5*7 = 126
``````

Should be:

``````2+4+5 = 11
3*7*6 = 126
``````

please write a simpler code without defining a function please...using loops. in 234 2 and 4 are even places(0th and 2nd resp.)and 3 is the odd one so sum=3 and product is 8.

Like @woooee said, your code is tackling the numbers backwards because of your use of `mod` to get the digits. You can fix that, or make n zero-based to shift the even/odd detection. Your code isn't far off. Also, basic code inside of a function can easily be made to work outside of a function. If this is a homework assignment about `while` loops we can't write the whole assignment for you. We're just trying to help you get where you need to be.

When dealing with user input from the console it comes in as a string. The most popular way to iterate over a string is a `for` loop. But if your teacher insist on using a `while` loop consider this example:

``````digits = '123456'
position = 0
# Save the length of the digits string.
digitlen = len(digits)
# Loop until position == digitlen
while position < digitlen:
# Grab the digit at the current position, convert to int.
value = int(digits[position])
# Use mod 2 to determine evens and odds.
if position % 2 == 0:
print('even: ({}) = {}'.format(position, value))
else:
print(' odd: ({}) = {}'.format(position, value))
position += 1
``````

This grabs each digit in order, and determines if it is even or odd. From there you can decide what to do in each case.

For any future searchers, it is the position in the string that is important. Note that the OP is checking "n" for even or odd, not the number itself. But the code can be easily modified to do either one.

Hint ...

``````digits = '123456'

size = len(digits)
# list of even indexed digits, positions 0,2,4, ...
evens = [int(digits[n]) for n in range(0, size, 2)]
# list of odd indexed digits
odds = [int(digits[n]) for n in range(1, size, 2)]

print(evens)  # [1, 3, 5]
print(odds)   # [2, 4, 6]
``````

Assume that your indexing is zero based.

Edited by vegaseat

Couldn't you simply do this:

``````digits = '234'

size = len(digits)

sum_odd = 0
for ix in range(1, size, 2):
sum_odd += int(digits[ix])

prod_even = 1
for ix in range(0, size, 2):
prod_even *= int(digits[ix])

print("sum of odd indexed integers = {}". format(sum_odd))
print("product of even indexed integers = {}". format(prod_even))
``````