I am trying to create a multiplication chart with a while loop for an assignment but I am having a hard time getting the expected output.
I have tried everything I can think of. I am new to the coding world.
#While loop
print('While Loop')
print()
x = 1
y = 1
z = 1
while x <= 12:
print(x ,end='\t')
x += 1
while y <= 12:
print('\n')
print(y,'\t')
y += 1
while z <= 12:
print(x*z ,end='\t')
z += 1
z = 0
x = 1
I expect the output to be
but I get
.
Short answer: you use x*z when you calculate the product, but you use y as a "row counter" and z as a "column counter" so it should be y*z. Furthermore you should increment the y after the inner while loop.
Since you use y as the "row counter" and z as the "column counter", you should print y * z as the answer of that specific multiplication. Furthermore you increment y too soon: you should increment it after the while loop, and reset z to 1, like:
print('While Loop')
print('x', end='\t') # print a cross
x = 1
while x <= 12:
print(x ,end='\t')
x += 1
y = 1
while y <= 12:
print('')
print(y,end='\t')
z = 1 # reset to 1
while z <= 12:
print(y*z ,end='\t') # use y * z
z += 1
y += 1 # incerement after while loop
print()
Using for loops
There are also some minor formatting issues. For example you should first print('X', end='\t') since otherwise the columns do not match correctly.
That being said, you make things way harder than these should be. You can make use of a for loop instead, like:
print('x', end='\t')
print('\t'.join(str(i) for i in range(1, 13)))
for r in range(1, 13):
print(r, end='\t')
print('\t'.join(str(r*c) for c in range(1, 13)))
or in a function:
def mulgrid(n):
print('x', end='\t')
print('\t'.join(str(i) for i in range(1, n+1)))
for r in range(1, n+1):
print(r, end='\t')
print('\t'.join(str(r*c) for c in range(1, n+1)))
For example:
>>> mulgrid(1)
x 1
1 1
>>> mulgrid(2)
x 1 2
1 1 2
2 2 4
>>> mulgrid(5)
x 1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
>>> mulgrid(7)
x 1 2 3 4 5 6 7
1 1 2 3 4 5 6 7
2 2 4 6 8 10 12 14
3 3 6 9 12 15 18 21
4 4 8 12 16 20 24 28
5 5 10 15 20 25 30 35
6 6 12 18 24 30 36 42
7 7 14 21 28 35 42 49
Related
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
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
Heyy, i got a bit of help earlier but am stuck once again :(
I am completely new to coding so i apologize for questions on such simple matters.
I am writing a program that puts out a multiplication table for the numbers that the user chooses(Using while instead of for to better understand how while loops work)
This is what i have so far:
print(end='')
max_x = int(input("Number of columns:"))
x = 1
while x <= max_x:
print(end='')
x += 1
max_y = int(input("Number of rows:"))
y = 1
while y <= max_y:
print('')
print(end='')
z = 1
while z <= max_x:
print(y*z, end='\t')
z += 1
y += 1
And this is the output that i get:
Number of columns:5
Number of rows:4
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
My issue is that i can´t for the life of me figure out how to get the table to also include the 1, as in:
1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
Appreciate any help as i´ve been trying and googling a bunch and frankly feel quite dumb struggling with such a simple thing
The extra digits are for the axis labels so they need to be drawn separately.
Try this code:
print(end='')
max_x = int(input("Number of columns:"))
x = 1
while x <= max_x:
print(end='')
x += 1
max_y = int(input("Number of rows:"))
y = 1
print(' \t' + '\t'.join([str(i+1) for i in range(max_x)]), end="") # labels X axis
while y <= max_y:
print('')
print(end='')
z = 1
print(y,end='\t') # label Y axis
while z <= max_x:
print(y*z, end='\t')
z += 1
y += 1
Output:
Number of columns:4
Number of rows:4
1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
I wrote this code to print out the multiplication table from 1 to 9, but it prints it out without a new line between the different tables. Does anyone know how to fix this?
for i in range(1, 10):
for j in range(1, 10):
k = i * j
print(i,"x",j, "=", k)
the result is this:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
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
Print an empty line between iterations of your for i in range(1, 10): loop, this will separate the tables by the number you're printing the multiplication of
for i in range(1, 4):
for j in range(1, 4):
k = i * j
print(i,"x",j, "=", k)
print()
>> 1 x 1 = 1
>> 1 x 2 = 2
>> 1 x 3 = 3
>> 2 x 1 = 2
>> 2 x 2 = 4
>> 2 x 3 = 6
>> 3 x 1 = 3
>> 3 x 2 = 6
>> 3 x 3 = 9
for i in range(1, 10):
for j in range(1, 10):
k = i * j
print(i,"x",j, "=", k, end='\n')
def tablesOneToTen(): # a function that will print out multiplication tables from 1-10
x = 1
y = 1
while x <= 10 and y <= 12:
f = x * y
print(f)
y = y + 1
x = x + 1
tablesOneToTen()
I am trying to make a function that will give me values from the multiplication table from 1-10.
Should I add if and elif statements in addition to nested while loops to make this code work?
For these sort of iteration tasks you're better off using the for loop since you already know the boundaries you're working with, also Python makes creating for loops especially easy.
With while loops you have to check that you are in range using conditionals while also explicitly incrementing your counters making mistakes all the more likely.
Since you know you need multiplication tables for values of x and y ranging from 1-10 you can, to get you familiar with loops, create two for loops:
def tablesOneToTen(): # a function that will print out multiplication tables from 1-10
# This will iterate with values for x in the range [1-10]
for x in range(1, 11):
# Print the value of x for reference
print("Table for {} * (1 - 10)".format(x))
# iterate for values of y in a range [1-10]
for y in range(1, 11):
# Print the result of the multiplication
print(x * y, end=" ")
# Print a new Line.
print()
Running this will give you the tables you need:
Table for 1 * (1 - 10)
1 2 3 4 5 6 7 8 9 10
Table for 2 * (1 - 10)
2 4 6 8 10 12 14 16 18 20
Table for 3 * (1 - 10)
3 6 9 12 15 18 21 24 27 30
With a while loop, the logic is similar but of course just more verbose than it need to since you must initialize, evaluate the condition and increment.
As a testament to its uglyness, the while loop would look something like this:
def tablesOneToTen():
# initialize x counter
x = 1
# first condition
while x <= 10:
# print reference message
print("Table for {} * [1-10]".format(x))
# initialize y counter
y = 1
# second condition
while y <=10:
# print values
print(x*y, end=" ")
# increment y
y += 1
# print a new line
print(" ")
# increment x
x += 1
Using Python 3
for i in range(1, 10+1):
for j in range(i, (i*10)+1):
if (j % i == 0):
print(j, end="\t")
print()
or:
for i in range(1, 10+1):
for j in range(i, (i*10)+1, i):
print(j, end="\t")
print()
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Hope it would help you to get 1 to 10 table.
a = [1,2,3,4,5,6,7,8,9,10]
for i in a:
print(*("{:3}" .format (i*col) for col in a))
print()