2

Code

a = int(input("enter a no"))
b = int(input("enter a range"))
for i in range(1, a+1):
    print(i)

    for j in range(1, b + 1):
        c = i * j
        print(i, "*", j, "=", c)

Desired output

1               2               3
1 * 1 = 1       2 * 1 = 2       3 * 1 = 3
1 * 2 = 2       2 * 2 = 4       3 * 2 = 6
1 * 3 = 3       2 * 3 = 6       3 * 3 = 9
1 * 4 = 4       2 * 4 = 8       3 * 4 = 12
1 * 5 = 5       2 * 5 = 10      3 * 5 = 15
1 * 6 = 6       2 * 6 = 12      3 * 6 = 18
1 * 7 = 7       2 * 7 = 14      3 * 7 = 21
1 * 8 = 8       2 * 8 = 16      3 * 8 = 24
1 * 9 = 9       2 * 9 = 18      3 * 9 = 27
1 * 10 = 10     2 * 10 = 20     3 * 10 = 30
1
  • 2
    Welcome to Stack Overflow. What happened when you tried running your code? How is that different from what is supposed to happen? What happened when you tried to fix the problem by changing the code? Commented Jul 21, 2021 at 19:15

4 Answers 4

1

You can tell print to finish with something other than a newline by specifying the "end" argument:

a = int(input("enter a no "))
b = int(input("enter a range "))

for i in range(1, a+1):
    print(i, end="            ")
print("")

for j in range(1, b + 1):
    for i in range(1, a+1):
        c = i * j
        print(i, "*", j, "=", c, end="    ")
    print("")

In this case I split the main loop into two separate loops, the first outputs the top line (1, 2, 3...) with some large spacing, whilst the second then does all of the others with slightly less spacing.

I also switched the order of the later loops since there should be b lines, each with a multiplications, so the b loop needs to be the outer (first) loop.

In both cases an empty print statement is used to output a newline when we need it.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is incomplete -- To improve it, you should explain how you changed OP's loops and why that was necessary. Simply adding an end argument to print() doesn't do what OP wants.
0

This is what you are looking for:

a = int(input("enter a no"))
b = int(input("enter a range"))

for i in range(1, a + 1):
    print(i, end="\t"*4)
print("")

for k in range(1, b + 1):
    for j in range(1, a + 1):
        print(j, "*", k, "=", j*k, end="\t"*2)
    print("")

You have to think line-by-line, since you cannot go back in stdout.

  • The first for will fill the first line
  • I modified your nested loop because you were using the wrong variables
  • "\t" is meant to get decent formatting, but it will eventually break if numbers are too big: I believe there is a more refined approach that I am not aware of
  • print("") is just meant to go to the next line

3 Comments

But, the first row of numbers does not align with the other numbers in the columns (for example a = 4, b = 10).
I know, as I said, it will break if numbers are big. The algorithm is still correct, so I posted it anyway.
There's no need for print(""), a plain print() will be enough.
0

Approach

  • Approach is use f-strings to left align all the prints into fields of width 20
  • Use fixed field widths since otherwise columns won't line up with single and multi-digit numbers
  • Use print parameter end = '' to avoid carriage returns on after printing a field

Code

a = int(input("enter a no ")) b = int(input("enter a range "))

width = 20
for i in range(1, a+1):
    print(f'{i:<{width}}', end="")       # Print all multipliers on a single row
print("")

for j in range(1, b + 1):
    # Looping over multiplication row
    for i in range(1, a+1):         # Looping through the columns to multipl
        s = f'{i} * {j} = {i*j}'    # Expression for column field
        print(f'{s:<{width}}', end = '') # Print field left aligned to width 20
    print("")                       # New row

Test

enter a no 4
enter a range 10
1                   2                   3                   4                   
1 * 1 = 1           2 * 1 = 2           3 * 1 = 3           4 * 1 = 4           
1 * 2 = 2           2 * 2 = 4           3 * 2 = 6           4 * 2 = 8           
1 * 3 = 3           2 * 3 = 6           3 * 3 = 9           4 * 3 = 12          
1 * 4 = 4           2 * 4 = 8           3 * 4 = 12          4 * 4 = 16          
1 * 5 = 5           2 * 5 = 10          3 * 5 = 15          4 * 5 = 20          
1 * 6 = 6           2 * 6 = 12          3 * 6 = 18          4 * 6 = 24          
1 * 7 = 7           2 * 7 = 14          3 * 7 = 21          4 * 7 = 28          
1 * 8 = 8           2 * 8 = 16          3 * 8 = 24          4 * 8 = 32          
1 * 9 = 9           2 * 9 = 18          3 * 9 = 27          4 * 9 = 36          
1 * 10 = 10         2 * 10 = 20         3 * 10 = 30         4 * 10 = 40    

Comments

0

I corrected it like this. Thankyou guys. just add the end="\t"*4 to fix the alignment issue.

a = int(input("enter a no "))
b = int(input("enter a range "))

for i in range(1, a+1):
    print(i, end="\t"*4)
print("")

for j in range(1, b + 1):
    for i in range(1, a+1):
        c = i * j
        print(i, "*", j, "=", c, end="\t"*2)
    print("")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.