How to print multiples of N in a range?

Asked

Viewed 12,868 times

1

The program must receive as input three values N, A, and B, and must print the multiples of N contained in the interval between A and B. I am doing it as follows, but it is going wrong:

N = int(raw_input())
A = int(raw_input())
B = int(raw_input())
x = range(A, B + 1, N)
try:
    for i in range(A, B + 1, N):
        if x == [1]:
            print 'INEXISTENTE'
        else:
            print i 
except:
    if x == [1]:
        print 'INEXISTENTE'

If there are no multiples of N in the given range, the program must print "NONEXISTENT". The code works for some values, but for others, for example 3, 5, 9 (N, A and B, respectively), it does not work. Another tried code:

N = int(raw_input())
A = int(raw_input())
B = int(raw_input())
x = range(A, B + 1, N)
for i in range(A, B + 1, N):
    if i % N == 0:
        print i

Also works for some values and for others not.

Updating:

N = int(raw_input())
A = int(raw_input())
B = int(raw_input())
x = range(A, B + 1)

for i in range(A, B + 1):
    if i % N == 0:
        print i
    elif x == []:
        print 'INEXISTENTE'

This last code doesn’t work either. If x has no values, that is, if there are no multiples in the range, the program should print 'NONEXISTENT', if the entries were, for example, 12, 1 and 10, where there are no multiples of 12 between 1 and 10.

4 answers

1

In Python there is a very nice operator who can help you in this task: %. It returns the rest of the division of one number by the other. Try:

>>> 4 % 2
0
>>> 5 % 2
1

That way, if you want to know if one number is multiple of another, just compare the result of the operation with 0:

if x % y == 0:
    print('é múltiplo')
else:
    print('não é múltiplo')

Therefore, a possible way to accomplish your exercise would be:

n = int(input())
a = int(input())
b = int(input())

for x in range(a, b):
    if x % n == 0:
        print(x)
    else:
        print('Inexistente')

Note: Change it input for raw_input and range by xrange if using Python 2.

  • Using its code, using as input: 3, 5, 9, it prints only the number 6, and also prints 'Nonexistent' three times. The expected output would be only 6 and 9, one on each line. 'Nonexistent' only applies to entries of type 12, 1, 10, where between 1 and 10 there are no multiples of 12.

  • I edited the question.

1


I got it this way:

N = int(raw_input())
A = int(raw_input())
B = int(raw_input())
for i in range(A, B + 1):
    if i % N == 0:
        print i
if N > B:
    print 'INEXISTENTE' 

Thanks for the tips!

0

You need to ensure that the beginning of your range is divisible by N:

A, B, N = [int(raw_input()) for _ in xrange(3)]
multiplos = xrange(A + N - (A % N), B + 1, N)
print '\n'.join(map(str, multiplos)) if len(multiplos) > 0 else 'INEXISTENTE'

0

Mine rolled like this in Python 3:

N = int(input())
A = int(input())
B = int(input())
for i in range(A, B + 1):
    if i % N == 0:
        print (i)
if N > B:
    print ('INEXISTENTE')
  • Consider explaining a little better why your code works.

Browser other questions tagged

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