Print "n" natural odd numbers

Asked

Viewed 15,010 times

4

I’m solving an exercise that asks me to enter a value for example: 5 and print the first 5 natural odd numbers in the case:

entree:

Enter the value of n: 5

  • 1
  • 3
  • 5
  • 7
  • 9

The code I wrote works but it does not seem to me the best logic for solving it. Would anyone know any other "simple" solution because I’m still learning

n = int(input("Digite o valor de n: "))

i = 1

while i <= (n+n):
if i % 2 != 0:
    print(i)
    i += 1
else:
    i += 1

7 answers

6

A very simple alternative would be with the function range(start, stop, step)

  • Start: initial sequence number.
  • Stop: up to this number, but does not include it in the sequence.
  • Step: difference between each number in the sequence.

    n = int(input("Digite o valor de n: "))
    for i in range(1, n+n, 2):
       print(i)
    

output:

inserir a descrição da imagem aqui

  • Thanks bro @Mathias

6


I was able to understand and apply with another logic without having to add n+n, in case it would look this way:

n = int(input("Digite o valor de n: "))

i = 0
ímpar = 1

while i < n: 
  print(ímpar)
  i = i + 1
  ímpar = ímpar + 2

1

It is also possible to solve the problem without using any iteration loop type, just unpacking the values of the sequence, generated with range(), using the operator * and passing them as arguments to function print().

In the example separator, parameter sep, will be a character newline:

n = int(input("Digite o valor de n: "))
print(*range(1, 2*n, 2), sep="\n")

Upshot:

Digite o valor de n: 5
1
3
5
7
9

Another approach manipulating the arguments of print() can be obtained by combining the function of the odd natural numbers f(x) = 2x + 1 | x N within a list comprehension

n = int(input("Digite o valor de n: "))
print(*[2*x+1 for x in range(n)], sep="\n")

1

Just to record another alternative (and I admit that maybe it’s a half Overkill, since the other solutions with range are simpler), you can use the module itertools:

from itertools import islice, count

n = int(input("Digite o valor de n: "))
for i in islice(count(1, 2), n):
    print(i)

First, count(1, 2) returns an iterator starting at 1, skipping 2 by 2 (thus, I guarantee it only has odd numbers, and always starting from the 1). Coming up next, islice receives this iterator and picks up the first ones n elements of it.

The result is the first n odd numbers.

  • 1

    You can also do it using repeat for i in repeat(count(1, 2), 5): print(next(i)).

1

I did so in Python, which includes the last number if it is odd.

num = int(input("Digite um número inteiro: "))

for num in range(1, num+1, 2):

    print(num)

0

To get the 5 first odd numbers:

    n = int(input("Digite o valor de n:"))

    # Inicializações
    i = 0 # Contador
    r = 1 # Resultado

    # Imprima os números ímpares
    while i < n:
       i += 1
       print(r)
       r += 2

-5

You can use the for cycle, which I like very much:

printf("Digite o numero");
int k = scanf(&k);

for(int x = 0; x < k; k++){
if(k%2 != 0){
printf("%i\n", k);
} // end if
} // end for
  • 3

    Please stick to all information of the question before answering. Python language help has been requested, so its response does not meet the needs.

Browser other questions tagged

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