Find Python Multiplication from user input - python

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

Related

College problem and I have no idea what to do (Prime numbers)

Im currently in college and I have an assignment that involves multiplying the primes and non primes below 45, so that their product doesn't go over 45. The language is python and I have tried doing for loops and checking for prime and not prime with modulus but I don't know where to go from there. (can't utilize arrays or anything related only loops, while else, elif etc.)
for num in range (1, 45):
if num > 1:
for num2 in range(2, 45):
if (num%num2) == 0:
n = num2
print(n, 'is Prime')
if num * num2 < 45:
print('Number ', num, '*', num2, '=', num*num2)
What exactly im trying to achieve is to first find all the prime numbers, then multiply them by all the non primes. Those primes and non primes are in the range of 1 - 45. Then, I Have to multiply them and their product has to be less than 45 then print that.
UPDATE: So I tried Thanh Tùng Nguyễn's program which is very well built, but I Can't utilize functions since I haven't been taught that (aka would be weird), what I tried to do is simply take what the function is and tried to just implement it into the loops themselves setting n as a boolean value, but my print doesn't amount to anything and I can't catch what's happening. The terminal just stays completely empty. My guess is on the last 'if not' statement, where I have probably written it wrong because I can't understand it myself why it isn't working when im treating it the same as a function through the entire block.
Here's what I got:
from math import sqrt
for num1 in range (2, 46):
if num1 <= 1:
n = False
for i in range (2, int(sqrt(num1))):
if num1%i == 0:
n = False
n = True
if n == True:
for num2 in range (1, 46):
if num2 <= 1:
n = False
for j in range (2, int(sqrt(num2))):
if num2%j == 0:
n = False
n = True
if not n == True:
if num1 * num2 < 45:
print(num1, '*', num2, '=', num1*num2)
You could create a prime-number check function like this:
from math import sqrt
def checkPrime(num):
if num <= 1:
return False
for i in range(2, int(sqrt(num))+1):
if num % i == 0:
return False
return True
Note that you can just check from 2 to √n to optimize it. From there, just find all prime numbers below 45 and multiply it with a non-prime number:
from math import sqrt
def checkPrime(num):
if num <= 1:
return False
for i in range(2, int(sqrt(num))+1):
if num % i == 0:
return False
return True
for n in range(2, 45+1):
if checkPrime(n):
for j in range(1, 45+1):
if not checkPrime(j):
if n * j < 45:
print(n, " * ", j, " = ", n*j)
Output:
2 * 1 = 2
2 * 4 = 8
2 * 6 = 12
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
2 * 12 = 24
2 * 14 = 28
2 * 15 = 30
2 * 16 = 32
2 * 18 = 36
2 * 20 = 40
2 * 21 = 42
2 * 22 = 44
3 * 1 = 3
3 * 4 = 12
3 * 6 = 18
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
3 * 12 = 36
3 * 14 = 42
5 * 1 = 5
5 * 4 = 20
5 * 6 = 30
5 * 8 = 40
7 * 1 = 7
7 * 4 = 28
7 * 6 = 42
11 * 1 = 11
11 * 4 = 44
13 * 1 = 13
17 * 1 = 17
19 * 1 = 19
23 * 1 = 23
29 * 1 = 29
31 * 1 = 31
37 * 1 = 37
41 * 1 = 41
43 * 1 = 43
If I understand correctly, you need to multiply primes with non-primes. This means that the products will be formed of 3 or more primes.
To have 3 primes in the product, you can have 2 at most 5 times. You can have 3 at most 3 times, 5 at most twice, 7/11 no more than once.
So you only need to take into account 2, 3, 5, 7 and 11 as primes and combine them in sets of 3 or more without going over 45:
You could nest 5 for-loops for the number of 2s, 3s, 5s, 7s and 11s in the product.
For example:
for p2 in range(7):
for p3 in range(4):
for p5 in range(3):
for p7 in range(2):
for p11 in range(2):
prod = 2**p2 * 3**p3 * 5**p5 * 7**p7 * 11**p11
if prod > 45 : continue
if p2+p3+p5+p7+p11 < 3: continue
if p2: print(2,'x',prod//2,'=',prod)
if p3: print(3,'x',prod//3,'=',prod)
if p5: print(5,'x',prod//5,'=',prod)
if p7: print(7,'x',prod//7,'=',prod)
if p11: print(11,'x',prod//11,'=',prod)
Output:
3 x 15 = 45
5 x 9 = 45
3 x 9 = 27
2 x 21 = 42
3 x 14 = 42
7 x 6 = 42
2 x 15 = 30
3 x 10 = 30
5 x 6 = 30
2 x 9 = 18
3 x 6 = 18
2 x 22 = 44
11 x 4 = 44
2 x 14 = 28
7 x 4 = 28
2 x 10 = 20
5 x 4 = 20
2 x 6 = 12
3 x 4 = 12
2 x 18 = 36
3 x 12 = 36
2 x 4 = 8
2 x 20 = 40
5 x 8 = 40
2 x 12 = 24
3 x 8 = 24
2 x 8 = 16
2 x 16 = 32à
You can also do it the other way around, by counting the primes that form each number up to 45 and, if there are 3 or more primes, print the ones that are part of the number as you go:
for prod in range(8,46):
primeCount = 0
rest = prod
for prime in range(2,12):
while rest and rest % prime == 0:
primeCount += 1
rest = rest//prime
if primeCount<3: continue
if prod%2==0: print(prod,'=',2,'x',prod//2)
if prod%3==0: print(prod,'=',3,'x',prod//3)
if prod%5==0: print(prod,'=',5,'x',prod//5)
if prod%7==0: print(prod,'=',7,'x',prod//7)
if prod%11==0: print(prod,'=',11,'x',prod//11)
Note that, even though I'm dividing by non=primes in the counting loop, only primes will actually be counted because, as the rest is reduced, multiples of previous primes will no longer divide the rest evenly.
Output:
8 = 2 x 4
12 = 2 x 6
12 = 3 x 4
16 = 2 x 8
18 = 2 x 9
18 = 3 x 6
20 = 2 x 10
20 = 5 x 4
24 = 2 x 12
24 = 3 x 8
27 = 3 x 9
28 = 2 x 14
28 = 7 x 4
30 = 2 x 15
30 = 3 x 10
30 = 5 x 6
32 = 2 x 16
36 = 2 x 18
36 = 3 x 12
40 = 2 x 20
40 = 5 x 8
42 = 2 x 21
42 = 3 x 14
42 = 7 x 6
44 = 2 x 22
44 = 11 x 4
45 = 3 x 15
45 = 5 x 9

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

While loop is not producing multiplication table [duplicate]

This question already has answers here:
Python check for integer input
(3 answers)
Closed 2 years ago.
i = 0
d = input("Enter the no. you want ")
while i < 11 :
print(i * d)
i+= 1
It is supposed to give multiplication table of d but it gives the following result for eg. '3' instead
3
33
333
3333
33333
333333
3333333
33333333
333333333
3333333333
The input() returns a string not an int and if you multiply a string in Python with an integer num, then you will get a string repeated num times. For example,
s = "stack"
print(s * 3) # Returns "stackstackstack"
You need to use int() constructor to cast the input from str to int.
d = int(input("Enter the no. you want "))
Try this:
d = int(input("Enter the no. you want "))
for i in range(1,11):
print(i * d)
In the above code, I have replaced your while loop construct with for loop and range() to get sequence of numbers.
BONUS:
To print/display the table in a nice and better way, try the below code:
for i in range(1,11):
print("%d X %d = %d" % (d, i, i * d))
Outputs:
Enter the no. you want 2
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20

Reverse Multiplication Table

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

creating a multiplication table in python with user input [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am extremely new to python so my question might seem a bit too trivial but believe me, its like rocket science right now for me. So, here it is. I have to create a program that reads single line of user input containing an integer, and print out the multiples of that number up to 12 times that number. For example:
Enter a number: 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36
I think I have to use for and range functions but don't know how to do that. Please help me :(
This is a really basic program,, all the above things are covered in basic python itself..
You should go through the following links like Bytes of Python, LearnPython etc
There are more tutorials for you to go through and get started..
This line takes the input from the user
In [8]: n = raw_input("Enter a number : ")
Enter a number : 5
This line iterates over the set of values 1 to 12, in python we can use range function for this..
range(1, 13) is there coz it range function stops before the last value..
%s is a string formatting syntax, similar to C, finally int(n) is coz inputs are in string format, need to convert it to int before multiplication can happen
In [9]: for i in range(1,13):
...: print "%s x %s = %s" %(n, i, i*int(n))
...:
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
5 x 11 = 55
5 x 12 = 60
>>> inp=int(raw_input('enter an integer'))
enter an integer 5
>>> for i in xrange(1,13): #use range() if you're on python 3.x
... print "{0} X {1} = {2}".format(inp,i,inp*i)
...
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
5 X 11 = 55
5 X 12 = 60
var = raw_input("Enter something: ")
print "you entered ", var
for n in range(1, 10):
prod = int(var)*n
print str(var) + " X " + str(n) + " = " + str(prod)
x = raw_input('Enter a number: ')
for i in range(1,12):
print int(x)*i
But you should check if x is really a number ;)
And here's an incomprehensible oneliner that will fit in a Tweet, because why not:
import itertools;print"\n".join("%d x %d = %d"%(x+(x[0]*x[1],))for x in itertools.product([int(raw_input("Enter a number : "))],range(1,13)))

Categories

Resources