Lucas Hampson
Lucas Hampson

Reputation: 11

Only print the integers?

I have this code:

for x in range (2,101):
    for y in range (2,101):
        print("{0}/{1}={2}".format(x,y,x/y))

It should print the prime numbers from 1 to 100, but it has all the numbers at the end that I want to get rid of. Say for example, 14.895789538953, I don't need/want those extra numbers at the end.

How to change it so it only prints the integers?

Upvotes: 0

Views: 108

Answers (1)

MSeifert
MSeifert

Reputation: 152715

Use the modulo operator % to check for the "remainder" of a division:

for x in range (2,101):
    for y in range (2,101):
        if x % y == 0:  # no remainder => x / y is an integer
            print("{0}/{1}={2}".format(x,y,x//y))

Upvotes: 2

Related Questions