Through Python, the output rectangle full format, the upper left triangle, the upper right triangle, the left lower triangle and the lower right triangle in five formats 99 multiplication table.
1. Rectangular full format
Code:
1 #完整格式输出九九乘法表2 for I in Range (1,10): 3 for J in Range (1,10): 4 print ("%d*%d=%2d"% (i,j,i*j), end= "") 5 print (" ")
Output content:
2. Upper Left Triangle
Code:
1 #左上三角格式输出九九乘法表2 for I in Range (1,10): 3 for J in Range (i,10): 4 print ("%d*%d=%2d"% (i,j,i*j), end= "") 5 Print ("") 6
Output content:
Note: Multiplication calculation by row output, compared with the full format, the inner loop range is i~9, when the outer loop of I gradually increment, the number of the output of each line will be less and fewer, print ("") for the line, do not output this sentence output multiplication table type of confusion.
3. Upper Right Triangle
Code:
1 #右上三角格式输出九九乘法表2 for I in Range (1,10): 3 for K in range (1,i): 4 print (end= " ") 5 for J in Range (i,10): 6
print ("%d*%d=%2d"% (i,j,i*j), end= "") 7 print ("")
Output content:
Note: Compared to the upper left triangle, the inner loop statement is more than two sentences (Code Red place), because each equation occupies a position of 7 bytes, so the extra space in front of the output of the corresponding number of spaces, in Python can not directly write print ("") statement to indicate the output space, you must add the end keyword, Represents the output ending with the right side of the equal sign, similar to the difference between the upper right and upper left of the following.
4. Lower left Triangle
Code:
1 #左下三角格式输出九九乘法表2 for I in Range (1,10): 3 for J in Range (1,i+1): 4 print ("%d*%d=%2d"% (i,j,i*j), end= "") 5 pri NT ("")
Output content:
5. Lower right triangle
Code:
1 #右下三角格式输出九九乘法表2 for I in Range (1,10): 3 for K in range (1,10-i): 4 print (end= " ") 5 for J in Range (1,i+1): 6 product=i*j7 Print ("%d*%d=%2d"% (i,j,product), end= "") 8 print ("")
Output content:
Python implements 99 multiplication tables in different formats