"Shell" Linux shell print 99 multiplication table detailed

Source: Internet
Author: User

Print 99 The multiplication table is a must-write program in any language, especially after learning the loop.

Print 99 multiplication Table First step

It is well known that the format of the 99 multiplication table is x * y = Z
So we need at least two parameters, one for x, one for y, here we use I and J to represent.
X and Y are not more than 9, and have a certain regularity, such as 1 x 2,1 x 3

根据上面的分析,我们首先需要把 i 和 j 先写出来,写就需要用到我们的双层for循环了。#!/bin/bash -for i in `seq 9`do    for j in `seq 9`        do        echo $j,$i    donedone

Execution results

[[email protected] scripts]# bash chengfa.sh 1,12,13,14,15,16,17,18,19,11,22,23,24,25,26,27,28,29,21,32,33,34,35,36,37,38,39,31,42,43,44,45,46,47,48,49,41,52,53,54,55,56,57,58,59,51,62,63,64,65,66,67,68,69,61,72,73,74,75,76,77,78,79,71,82,83,84,85,86,87,88,89,81,92,93,94,95,96,97,98,99,9[[email protected] scripts]#

Here, you may have found a little bit of regularity, I and J we've got it, so we're going to do a multiplication for I and J.

!/bin/bash -for i in `seq 9`do    for j in `seq 9`        do        echo "$i x $j = `echo $(($i*$j))`"      # 运算    donedone

Execution results

[[email protected] scripts]# bash chengfa.sh 1 x 1 = 11 x 2 = 21 x 3 = 31 x 4 = 41 x 5 = 51 x 6 = 61 x 7 = 71 x 8 = 81 x 9 = 92 x 1 = 22 x 2 = 42 x 3 = 62 x 4 = 82 x 5 = 102 x 6 = 122 x 7 = 142 x 8 = 162 x 9 = 183 x 1 = 33 x 2 = 63 x 3 = 93 x 4 = 123 x 5 = 153 x 6 = 183 x 7 = 213 x 8 = 243 x 9 = 274 x 1 = 44 x 2 = 84 x 3 = 124 x 4 = 164 x 5 = 204 x 6 = 244 x 7 = 284 x 8 = 324 x 9 = 365 x 1 = 55 x 2 = 105 x 3 = 155 x 4 = 205 x 5 = 255 x 6 = 305 x 7 = 355 x 8 = 405 x 9 = 456 x 1 = 66 x 2 = 126 x 3 = 186 x 4 = 246 x 5 = 306 x 6 = 366 x 7 = 426 x 8 = 486 x 9 = 547 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 357 x 6 = 427 x 7 = 497 x 8 = 567 x 9 = 638 x 1 = 88 x 2 = 168 x 3 = 248 x 4 = 328 x 5 = 408 x 6 = 488 x 7 = 568 x 8 = 648 x 9 = 729 x 1 = 99 x 2 = 189 x 3 = 279 x 4 = 369 x 5 = 459 x 6 = 549 x 7 = 639 x 8 = 729 x 9 = 81

After this step, the whole embryonic form will appear, and then we will find a problem, that is, each formula is a separate line, so we need to modify some programs, Echo's-n parameter is to cancel the cross-line output.

修改部分 echo -n "$i x $j = `echo $(($i*$j))`"

Execution results

After the step, you will find that all the variables are next to each other, how to change it? All we need to do is add two spaces to the back of ECHO.

修改部分 echo -n "$i x $j = `echo $(($i*$j))`  ”     # ”前面加了两个空格。

Execution results

  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 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 2 x 6 = 2 x 7 = 2 x 8 = 2 x 9 = 3 1 x 3 = 3 2 x 6 =  9 3 x 4 = 3 x 5 = 3 x 6 = 3 x 7 = 3 x 8 = 3 x 9 = 4 x 1 = 4 x 4 = 2 8 x 4 = 3 x 4 = 4 16 X 5 = 4 x 6 = 4 x 7 = 4 x 8 = 4 x 9 = 5 x 1 = 5 5 x 2 = 5 x 3 = 5 x 4 = 5 x  6 = 5 x 7 = 5 x 8 = 5 x 9 = 6 x 1 = 6 6 x 2 = 6 x 3 = 6 x 4 = 6 x 5 = 6 x 6 =   6 x 8 = 6 x 9 = 7 x 1 = 7 7 x 2 = 7 x 3 = 7 x 4 = 7 x 5 = 7 x 6 = 7 x 7 = 7  7 x 9 = 8 x 1 = 8 8 x 2 = 8 x 3 = 8 x 4 = 8 x 5 = 8 x 6 = 8 x 7 = 8 x 8 = 8 9 X 1 = 9 9 x 2 = 9 x 3 = 9 x 4 = 9 x 5 = 9 x 6 = Si 9 x 7 = 9 x 8 = 9 x 9 = Bayi  

Much better, but we will also find that the output will not wrap, because the 99 multiplication table is required to wrap, 1 x 1 = 21 rows, 1 x 2 =2 2 x 2 = 41 rows. So we need to keep the program going, for example, we're going to change the line every time the J loop ends.

修改部分for i in `seq 9`do    for j in `seq 9`        do        echo -n "$i x $j = `echo $(($i*$j))`  "    doneecho ""   # 打印空行don

Execution results

[[email protected] scripts]# bash chengfa.sh 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 = 2 x 6 = 2 x 7 = 2 x 9 = 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 3 x 5 = 3 x 6 = 3 x 7 = 3 x 8 = 3 x 9 =  4 4 x 2 = 8 4 x 3 = 4 x 4 = 4 x 5 = 4 x 6 = 4 x 7 = 4 x 8 = 4 x 9 = 5 1 x 5 = 5 2 X 3 = 5 x 4 = 5 x 5 = 5 x 6 = 5 x 7 = 5 x 8 = 5 x 9 = 6 1 x 6 = 6 x 2 = 6 x  4 = 6 x 5 = 6 x 6 = 6 x 7 = 6 x 8 = 6 x 9 = x 7 = 1 7 x 7 = 2 x 7 = 3 x 7 = 4 x 7 =   7 x 6 = 7 x 7 = 7 x 8 = 7 x 9 = 8 x 1 = 8 8 x 2 = 8 x 3 = 8 x 4 = 8 x 5 = 8  8 x 7 = 8 x 8 = 8 x 9 = 9 x 1 = 9 9 x 2 = 9 x 3 = 9 x 4 = 9 x 5 = 9 x 6 = 9 7   X 8 = 9 x 9 = 81

At this time we will find that our normal multiplication table is right triangle, now is the rectangular output, for what? Everyone may have found out, because we have part of the formula is repeated, if 1 x 2 = 2 and 2 x 1 = 2 Repeat, then how to solve it? You can find a rule, 1 x 2 =2, 2 x 2 = 4, and 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9, in the same row, the preceding number will not be greater than the subsequent number, i.e. J x i = Z,j will not be greater than I

修改部分for i in `seq 9`do    for j in `seq 9`        do        [ $j -le $i ] && echo -n "$i x $j = `echo $(($i*$j))`  "   # 如果 j 小与等于 i 才会打印式子    doneecho ""done

Execution results

[[email protected] scripts]# bash chengfa.sh 1 x 1 = 1  2 x 1 = 2  2 x 2 = 4  3 x 1 = 3  3 x 2 = 6  3 x 3 = 9  4 x 1 = 4  4 x 2 = 8  4 x 3 = 12  4 x 4 = 16  5 x 1 = 5  5 x 2 = 10  5 x 3 = 15  5 x 4 = 20  5 x 5 = 25  6 x 1 = 6  6 x 2 = 12  6 x 3 = 18  6 x 4 = 24  6 x 5 = 30  6 x 6 = 36  7 x 1 = 7  7 x 2 = 14  7 x 3 = 21  7 x 4 = 28  7 x 5 = 35  7 x 6 = 42  7 x 7 = 49  8 x 1 = 8  8 x 2 = 16  8 x 3 = 24  8 x 4 = 32  8 x 5 = 40  8 x 6 = 48  8 x 7 = 56  8 x 8 = 64  9 x 1 = 9  9 x 2 = 18  9 x 3 = 27  9 x 4 = 36  9 x 5 = 45  9 x 6 = 54  9 x 7 = 63  9 x 8 = 72  9 x 9 = 81  

This is over.
All rights reserved arppinging

"Shell" Linux shell print 99 multiplication table detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.