Receive two numbers, add the pairs and multiply the odd

Asked

Viewed 266 times

0

The program should receive two numbers, and then add all pairs between the received range, and multiply all the odd numbers of the received range (including those typed, always).

Must be used for or while

How to present the result?

For example, imagining that the user entered the values 2 and 8, then the output would be:

2 + 4 + 6 + 8 -> 20

3 * 5 * 7 -> 105

My code goes below:

numero = 0
contador = 1

for numero in range(numero, 3, 2):
    numero = int(input(f'Informe um número {contador}/2: '))
    contador = contador + 1
while numero % 2 == 0:
    for numero in range(numero, numero + numero):
        print(numero)
while numero % 2 != 0:
    for numero in range(numero, numero * numero):
        print(numero)

2 answers

1


User inserts start and end value:

valor1 = int(input())
valor2 = int(input())

Stores the sum of pairs and the multiplication of impairments:

par = 0
impar = 1

Here the loop for iterates over the initial value and the end + 1:

for numero in range(valor1, valor2 + 1):
    if numero % 2 == 0:
        par += numero
    else:
        impar *= numero    

Impression of values:

print(f'par: {par}, impar: {impar}')

0

The idea is to make a function that returns the values, very generic.

 def somaParMultImpar(numero1,numero2):
     
     impar = 1
     par = 0
     while(numero1<=numero2):
         if(numero1%2 == 0):
           par+=numero1
         else:
            impar*=numero1
         numero1+=1
     return impar, par
print(somaParMultImpar(2,10))
  • Thanks friend. I ended up opting for the code of the colleague above, because I managed to understand it better. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.