Home > Software design >  Finding all odd numbers divisible by 5
Finding all odd numbers divisible by 5

Time:05-29

I'm trying to code a program which shows odd numbers that are divisible by 5, but it isn't working. Can someone tell me what I did wrong?

def show_odd_numbers(min, max):     #this program shows odd numbers divisible by 5.      
    show_odd_numbers = range(5,81)     
    for numbers in range(min,max):       
        if numbers % 5 == 0:         
            show_odd_numbers.count     
    print(show_odd_numbers)

CodePudding user response:

There's several issues: you're not checking that the number is odd, you're not using min and max (which aren't variable names you should be using, since those are the names of built-ins), and .count is probably not something you're really looking for. Here is a code snippet that resolves all of these issues:

def show_odd_numbers(lower, upper):
    for number in range(lower, upper   1):       
        if number % 5 == 0 and number % 2 == 1:         
            print(number)
            
show_odd_numbers(5, 81)

This outputs:

5
15
25
35
45
55
65
75

CodePudding user response:

You can write a code which returns the odd numbers divisible by 5 on a range. The code you need:

import numpy as np
def show_odd_numbers(min, max):     #this program shows odd numbers divisible by 5. 
    results = []
    for number in np.arange(min,max):       
        if number % 5 == 0 and number % 2 ==1:         
            results.append(number)
    return(results)

The apply the function

show_odd_numbers(1,20)

Output:

[5,15]
  • Related