Home > Back-end >  Simple Small Multiplication table program in Python
Simple Small Multiplication table program in Python

Time:02-14

rows = int(input("Enter number: "))

print('1\t2\t3')
print('********************')
for i in range(1,rows 1):
    print(i*1,end='')
    print('\t',i*2,end='')
    print('\t',i*3)

Usually end="" makes not jumping in a new line. But with under print('\t',i*2,end=''). Everything is in vertical line . And I want it to be this way . But i dont get it if end="" makes it al horizonal why is it vertical?

CodePudding user response:

Consider using f-strings to align the columns dynamically:

num_rows = int(input('Enter number: '))
num_cols = 3
align = '<'
col_gap = 1
col_width = len(str(num_rows * num_cols))   col_gap
print(''.join(f'{c:{align}{col_width}}' for c in range(1, num_cols 1)))
print('*' * (col_width * num_cols - col_gap))
for r in range(1, num_rows 1):
    print(''.join(f'{r*c:{align}{col_width}}' for c in range(1, num_cols 1)))

Example Usage 1:

Enter number: 12
1  2  3  
********
1  2  3  
2  4  6  
3  6  9  
4  8  12 
5  10 15 
6  12 18 
7  14 21 
8  16 24 
9  18 27 
10 20 30 
11 22 33 
12 24 36 

Example Usage 2:

Enter number: 123
1   2   3   
***********
1   2   3   
2   4   6   
3   6   9   
4   8   12  
5   10  15  
6   12  18  
7   14  21  
8   16  24  
9   18  27  
10  20  30  
11  22  33  
12  24  36  
13  26  39  
14  28  42  
15  30  45  
16  32  48  
17  34  51  
18  36  54  
19  38  57  
20  40  60  
21  42  63  
22  44  66  
23  46  69  
24  48  72  
25  50  75  
26  52  78  
27  54  81  
28  56  84  
29  58  87  
30  60  90  
31  62  93  
32  64  96  
33  66  99  
34  68  102 
35  70  105 
36  72  108 
37  74  111 
38  76  114 
39  78  117 
40  80  120 
41  82  123 
42  84  126 
43  86  129 
44  88  132 
45  90  135 
46  92  138 
47  94  141 
48  96  144 
49  98  147 
50  100 150 
51  102 153 
52  104 156 
53  106 159 
54  108 162 
55  110 165 
56  112 168 
57  114 171 
58  116 174 
59  118 177 
60  120 180 
61  122 183 
62  124 186 
63  126 189 
64  128 192 
65  130 195 
66  132 198 
67  134 201 
68  136 204 
69  138 207 
70  140 210 
71  142 213 
72  144 216 
73  146 219 
74  148 222 
75  150 225 
76  152 228 
77  154 231 
78  156 234 
79  158 237 
80  160 240 
81  162 243 
82  164 246 
83  166 249 
84  168 252 
85  170 255 
86  172 258 
87  174 261 
88  176 264 
89  178 267 
90  180 270 
91  182 273 
92  184 276 
93  186 279 
94  188 282 
95  190 285 
96  192 288 
97  194 291 
98  196 294 
99  198 297 
100 200 300 
101 202 303 
102 204 306 
103 206 309 
104 208 312 
105 210 315 
106 212 318 
107 214 321 
108 216 324 
109 218 327 
110 220 330 
111 222 333 
112 224 336 
113 226 339 
114 228 342 
115 230 345 
116 232 348 
117 234 351 
118 236 354 
119 238 357 
120 240 360 
121 242 363 
122 244 366 
123 246 369
  • Related