Home > OS >  How to get desired result in the final answer
How to get desired result in the final answer

Time:10-10

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.

CodePudding user response:

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)}")

CodePudding user response:

  • 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

CodePudding user response:

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.

  • Related