Reverse Multiplication Table - python

I'm having troubling reversing my multiplication table.
This is what I have so far:
def reverseTable(n):
for row in range(1, n+1):
print(*("{:3}".format(row*col) for col in range(1, n+1)))
But I want to reverse it to:
25 20 15 10 5
20 16 12 8 4
15 12 9 6 3
10 8 6 4 2

You need to reverse your range so it counts backwards. The range() function accepts 3 parameters, range(start, stop, step) so to count from 10 to 1 you would use range(10, 0, -1)
Try this:
def reverseTable(n):
for row in range(n, 0, -1):
print(*("{:3}".format(row*col) for col in range(n, 0, -1)))

for row in range(9,0,-1):
print(end="\t")
for column in range(9,0,-1):
print(row*column,end="\t ")
print()

reverse multiplication table in python taking value from user
num = int(input("enter the number= "))
i=10
while i>=1:
print(num,"X",i,"=",num*i)
i= i-1
output
enter the number= 3
3 X 10 = 30
3 X 9 = 27
3 X 8 = 24
3 X 7 = 21
3 X 6 = 18
3 X 5 = 15
3 X 4 = 12
3 X 3 = 9
3 X 2 = 6
3 X 1 = 3

num=int(input("enter your table number: "))
for i in range(10,0,-1):
print(str(num) + "x" + str(i) + "=" + str(num*i))

Here's your answer
num=int(input("Enter the number"))
for i in range (10,0,-1):
print(f"{num}*{i}={num*i}")

num = int(input("enter a no."))
count = 10
while count >= 1:
X = num*count
print(num, "x", count, "=", X)
count = count - 1

i used this i dont know if it is of any help to you since I am a beginner and have just started learning python
num= int(input("please enter the number: "))
n= 11
for i in range(0,10):
n -= 1
print(f"{num} x {n} = {num*n} ")

Related

Find Python Multiplication from user input

Junior Python Coder here! Trying to print multiplication table with user input but stuck.
min_num=1
max_num=11
starting_range = int(input ("Enter the minimum number: "))
ending_range = int(input ("Enter the maximum number: "))
print ("The Multiplication Table of: ", starting_range)
for count in range(1, 11):
print (starting_range, 'x', count, '=', ending_range * count)
if max_num - min_num > 10 :
print('invalid range ')
else:
for num in range (min_num,max_num):
print ("The Multiplication Table of: ", ending_range)
for count in range(min_num, max_num):
print (starting_range, 'x', count, '=', ending_range * count)
I'm not sure I really understand what you're trying to do as your code isn't formatted, but for a multiplication table, a nested loop is a solution.
The basic idea is: For every number in the given range, loop over the whole range and multiply it by each element. This will print the whole multiplication table.
start = 1 # You can change these to be inputted by the user.
end = 10
for i in range(start, end + 1): # We add 1 to the end because range() is exclusive on endpoint.
for j in range(start, end + 1):
print(f"{i} x {j} = {i * j}")
If you only need the table as something like:
15 x 1
15 x 2
15 x 3
...
You can do this with one loop:
num = 10
for i in range(1, num + 1):
print(f"{num} x {i} = {num * i}")
I recommend you search up on F-strings in Python if you do not understand the print(f"..") parts. They're very convenient. Good luck!
Minimal change to original code:
min_num = 1
max_num = 11
starting_range = 4 # You can use int() and input() as in your original code to make this user defined.
ending_range = 7
# Removed the error checking, wasn't sure precisely what it was meant to do.
for num in range(starting_range,ending_range):
print ("The Multiplication Table of: ", num)
for count in range(min_num, max_num):
print (num, 'x', count, '=', num * count)
Try it at https://www.mycompiler.io/view/7JVNtxkT53k
This prints:
The Multiplication Table of: 4
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
The Multiplication Table of: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
The Multiplication Table of: 6
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
Alternative code defining a function:
def print_multiplication_table(n, max_value=11):
if n > max_value or n <= 0:
raise ValueError
print(f"The Multiplication Table of: {n}")
for m in range(1, max_value):
print(f"{n} x {m} = {n*m}")
starting_range = 4 # You can use int() and input() as in your original code to make this user defined.
ending_range = 7
for i in range(starting_range, ending_range):
print_multiplication_table(i)
Try it at https://www.mycompiler.io/view/1uttFLmFEiD

Making a 2D array without brackets

I'm trying to create a 2D array, but it prints out in one row and with brackets.
How do I make it into rows and columns without brackets?
rows = int(input("Please enter the number of rows: "))
board = [[i + (j * cols) for i in range(1, cols + 1)] for j in range(0, rows)]
print(board)
for row in board:
print(" ".join(list(map(str, row))))
We iterate through the board and for each row, make it a list of strings (using map) instead of numbers and join them with a space.
Maybe this is what you are looking for:
for row in board:
print(row)
Is this what you mean?
rows = int(input("Please enter the number of rows: "))
cols=rows#my thing would not run without this line
board = [[i + (j * cols) for i in range(1, cols + 1)] for j in range(0, rows)]
print(board)
arr=[]
for i in range(rows):
for c in range(cols):
print(str(i)+" "+str(board[i][c]))
Output:
0 1
0 2
0 3
0 4
0 5
1 6
1 7
1 8
1 9
1 10
2 11
2 12
2 13
2 14
2 15
3 16
3 17
3 18
3 19
3 20
4 21
4 22
4 23
4 24

sum of Fibbonaci Sequences?

Trying to add the sum of Fibonacci, using definite loops. It's meant to calculate the summation of Fibonacci number with each number too. Below is the sample for the Fibonacci sequence and its summation, how do i add the sum of the fibonacci eg 1,1,2,3,5,8
Fibonacci Summation
0 0
1 1
1 2
2 4
3 7
5 12
8 20
n = int(input("enter"))
def fibonacciSeries():
a=0
b=1
for i in range (n-2):
x = a+b
a=b
b=x
int(x)
x[i]= x+x[i-1]
#should add the previous sequences
print(x)
fibonacciSeries()
You don't need to keep track of the whole sequence. Plus your Fibonacci implementation doesn't start with 1, 1 but rather 1, 2 so I fixed that.
def fibonacciSeries(n):
a=0
b=1
x=1
series_sum = 0
for i in range (n-2):
series_sum += x
print(f'{x} {series_sum}')
x = a+b
a=b
b=x
n = 10
fibonacciSeries(n)
Output:
1 1
1 2
2 4
3 7
5 12
8 20
13 33
21 54
def fibonacciSeries(n):
sum = 0
a = 0
b = 1
x = 1
sum = 0
for i in range(0,n - 2):
sum += x
print(x,sum)
x = a + b
a = b
b = x
n = int(input("enter : ")) # n = 8
fibonacciSeries(n)
Output:
enter : 8
1 1
1 2
2 4
3 7
5 12
8 20

Trying to count from 10 to the input value provided and columns provided in Python but not getting it. I basically want like 5 numbers on top etc

counter = 10
numbers = int(input("Enter a number between 10 and 99: "))
column = int(input("How many columns would you like? "))
for num in range(10, numbers):
for col in range(column):
counter += 1
print(num + 1, end= ' ')
print()
Trying to count from 10 to the input value provided and columns provided in Python but not getting it. I basically want like 5 numbers on top, 5 on bottom etc.
counter = 10
numbers = int(input("Enter a number between 10 and 99: "))
column = int(input("How many columns would you like? "))
output_string = ""
col_counter = 0
while (counter <= numbers):
output_string += str(counter)+" "
counter += 1
col_counter += 1
if(col_counter == column):
print(output_string)
output_string=""
col_counter = 0
print(output_string)
This should do it just fine
do you want something like this?
>>> n = 30 # numbers
>>> c = 3 # columns
>>> for i in range(10, n+1):
... print(i, end='\t')
... if (i - 10) % c == 0:
... print()
... else:
... print()
...
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
>>>

For loop a square pattern of numbers

In this question I had to create a program that prompts the user for a number and then prompt again for how many rows to create. Something like:
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
This is what I came up with and I have tried many different ways to get the same result but it didn't work.
num=int(input("Enter a number between 1 and 10: "))
rows=int(input("Enter how many rows to of numbers: "))
for i in range(num):
print(i,end=" ")
for x in range(rows):
print (x)
This is the output I came up with:
Enter a number between 1 and 10: 6
Enter how many rows to of numbers: 4
0 1 2 3 4 5 0
1
2
3
You may simply do it like:
num = 5
rows = 4
for i in range(1, num+1):
print('{} '.format(i) * rows)
Output:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Explanation: When you multiply a str with some number say n, new string is returned with original string repeated n times. Doing this you will eliminate your nested loop
Simple solution: Just use nested for loops:
num = int(input("Enter a number between 1 and 10: "))
rows = int(input("Enter how many rows to of numbers: "))
for i in range(num):
print ('\n')
for x in range(rows):
print (i + 1)
The code above will go through the range 0 to num, printing first a new line and then printing the current number rows times.
rows = 5
side = int(input("Please Enter any Side of a Square : "))
for i in range(side):
for j in range(side):
if(i == 0 or i == side - 1 or j == 0 or j == side - 1):
print(i, end = ' ')
else:
print(i, end = ' ')
print()

Categories

Resources