kanuki
kanuki

Reputation: 111

Program that finds a numbers divisor

I've been making my own little program that finds the divisors for an inputted number.

The way it determines if a number can be divided with another number is if the result is an integer. If its a float, it should come out negative.

This is what I've got so far:

# Divider

Number = input("> ")

divider = 1

while True:
    Number = int(Number)
    divider = int(divider)

    result = 0

    result = int(result)

    result = Number/divider

    if isinstance(result, int):    
        print("{} Y".format(divider))

    else:
        print("{} N".format(divider))

    divider = divider + 1

    if divider == Number + 1:
        break

The problem is that when I run the program and input a number like "10" which should have more than one divisor (1,2,5,10) it comes out completely negative:

> 10
1 N
2 N
3 N
4 N
5 N
6 N
7 N
8 N
9 N
10 N
>>>

I wonder what I'm doing wrong.

Upvotes: 0

Views: 365

Answers (6)

Rohit Kumar
Rohit Kumar

Reputation: 1

num = int(input('Enter a number : '))
div = 1
count = 0
while True:
    if(div == num+1):
        break
    result = num%div
    if result == 0:
        print('{} -- yes'.format(div))
        count+=1
    else:
        print('{} -- No'.format(div))
    div+=1
print('Total number of divisor : ', count)

Upvotes: 0

Alfe
Alfe

Reputation: 59426

for divisor in range(1, number//2+1):
  print("{} {}".format(divisor, "Y" if number % divisor == 0 else "N"))

Some additional advice:

  • Don't use capitals for variable names. It's good practice to name classes with capitals instead (and all-caps for constants).
  • It is enough to walk until number // 2 (the integer division) because between number // 2 and number there cannot be any more divisors.
  • DRY - don't repeat yourself (a very general rule in programming): Use the a if b else c operator to avoid repeating the print.

Upvotes: 1

jar
jar

Reputation: 2908

I would solve it using modulus. If the remainder is 0 then its divisible otherwise its not. No need to have any int or float checks.

num = input("> ")
#Assuming the num is an integer
divisors = [] #List of divisors
for i in range(1,num):
  if num%i == 0:
    divisors.append(i)

Output:

>>[1, 2, 5]

Upvotes: 2

Wazaki
Wazaki

Reputation: 899

What about you just use modulo like in the following?

result = Number%divider

if result==0:   
    print("{} Y".format(divider))

Upvotes: 0

blhsing
blhsing

Reputation: 106523

The division operator / always results in a floating number in Python 3, so the result will never be an instance of an integer.

You should instead use the modulo operator to test if the remainder is 0:

if number % divider == 0:
    print("{} Y".format(divider))
else:
    print("{} N".format(divider))

Upvotes: 3

Mathieu
Mathieu

Reputation: 5746

First I'm going to clean up your code:

Number = 15

divider = 1

while True:

    if divider == Number + 1:
        break

    Number = int(Number)
    divider = int(divider)

    result = Number/divider

    if isinstance(result, int):
        print("{} Y".format(divider))

    else:
        print("{} N".format(divider))

    divider = divider + 1

Now, what are they all return as negative? Simply because the / division returns a float (i.e. result is a float). What is the correct solution? Use the % modulo to check if the remainder is 0.

Number = 15

divider = 1

while True:

    if divider == Number + 1:
        break

    Number = int(Number)
    divider = int(divider)

    result = Number%divider

    if result == 0:
        print("{} Y".format(divider))

    else:
        print("{} N".format(divider))

    divider = divider + 1

Output:

1 Y
2 N
3 Y
4 N
5 Y
6 N
7 N
8 N
9 N
10 N
11 N
12 N
13 N
14 N
15 Y

Upvotes: 3

Related Questions