# The Odd terms in a Fibonacci series using Python

First of all, let us suppose for some reason, you have to print out all the odd terms in a Fibonacci series. Well, this program will be a solution to that problem.

In this tutorial, you will learn how to print all the odd terms in a Fibonacci series using the Python programming language. In return what the program demands is the range up to which, you would want them to be printed. The program is short and simple.

### Odd ASCII

first = 0 second = 1 iteration = 0 length=int(input("Enter the range of elements: ")) if length <= 0: print("Please provide a number greater than zero") elif length == 1: print(f"This Fibonacci sequence has {length} element :") print(first) else: print(f"This Fibonacci sequence has {length} elements :") while iteration < length: next = first + second first = second second = next if (next%2!=0): print(f'{next} \n ') iteration += 1

### Output

After running the above Python program, you will be able to see the output given below:

Enter the range of elements: 3 This Fibonacci sequence has 3 elements : 1 3 5 Enter the range of elements: 4 This Fibonacci sequence has 4 elements : 1 3 5 13 Enter the range of elements: 5 This Fibonacci sequence has 5 elements : 1 3 5 13 21

#### Explanation:

The code begins with initializing variables ‘first’ and ‘second’ with 0 and 1 respectively. The program then gets the range of the number of terms from the user; and sets conditions. That is if range is equal to or less than 0, we print out, to provide a number greater than zero.

However if the range equals one we print only the first variable. Else we use a while loop and take out the sum of variables first and second. If the value of sum is an odd one we print out the value of sum and iterate the base condition.

## Leave a Reply