Home>

I wanted to solve the problem, it turned out correctly but not as it should. Problem: Print all the numbers on the segment from a to b that give the remainder of c when divided by d. If such numbers do not exist, then you do not need to display anything. Input: 2 5 0 2. In Output: 2 4. In general, my code:

a= int (input ())
b= int (input ())
c= int (input ())
d= int (input ())
sum= 0
for i in range (a, b + 1):
   if i% d== c:
       print (i)

In output, I got: 2 and below 4, but it should be so that 2 and 4 are on the same line and with a space. How to do it? Whoever says the answer will give a back massage

  • Answer # 1

    do you need this?

    a, b, c, d= map (int, input (). split ())
    print (* [i for i in range (a, b + 1) if i% d== c])
    

    here input and output will be in 1 line

  • Answer # 2

    Give print a space as the end of the line (instead of a line feed).

    for i in range (a, b + 1):
       if i% d== c:
           print (i, end= "")
    print ()
    

    what is this end= "" and why at the end of print ()? And so everything is correct

    Аянат Абилдинов2021-11-25 12:43:48

    end= "" is an instruction to print to print a space as the end of a line (instead of a line feed). And at the end, the usual print () to print the line feed.

    GrAnd2021-11-25 12:46:01