How to get desired result in the final answer

Question:

I was making a code to find the number divisible by x in a certain range and for that this is the code I wrote:

num = int(input("Enter your number: "))

for i in range(1,301):
    if i % num == 0:
        print(i)

I was able to get the result, however then I wanted to get the final print in this format:

13 / 13 = 1

26 / 13 = 2

and so on for any number chosen by the user. Please guide me how to get the result that way.

Asked By: Swarit Gaur

||

Answers:

Can you try this:


num = int(input("Enter your number: "))

for i in range(1,301):

    if i % num == 0:

       print(f"{i} / {num} = {int(i/num)}")

Answered By: White Hat Hacker
  • num divisible by i.
num = int(input("Enter your number: "))

for i in range(1,301):

    if num % i == 0:
       
       print(f'{num} / {i} = {num//i}')

more about f-strings

The answer would look like this:

for i in range(1, 301):
    if i%num ==0:
        result = i/num
        print("%i/%i=%i"%(i,num,result))

You would just format the values inside the answer.

Answered By: solembum
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.