For an even number of 100 or less
Idea: 1. Define a variable x first
The range of 2.x is 0-50,x with an initial value of 1
3. And the value initial value is 0, each time the loop and the value is equal to itself +2*i
Method One:
Vim sum1.sh
#!/bin/bash
#written by Lizheng
#about sum
Sum=0
For x in {1..50}; Do
sum=$ (($sum +2* $x))
Done
echo "The sum is $sum"
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/8B/67/wKioL1hNdoCj4X6IAAAX3ygU5DA499.png-wh_500x0-wm_3 -wmp_4-s_3295756640.png "style=" Float:none; "title=" 31.png "alt=" Wkiol1hndocj4x6iaaax3ygu5da499.png-wh_50 "/>
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/8B/6A/wKiom1hNdoHjsJQdAAAGaPmIrro971.png-wh_500x0-wm_3 -wmp_4-s_629407505.png "style=" Float:none; "title=" 32.png "alt=" Wkiom1hndohjsjqdaaagapmirro971.png-wh_50 "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Method Two: seq is a fixed notation
Idea: 1. First define a variable i
2. Let the value of sum first start with the value of 0,i from 1 to 1001.
3. Let I equals a number to remove to 2 if equal to 0, is the number we want to let them add together can get the sum needed
Vim sum2.sh
#/bin/bash
#written by Lizheng
#about sum
Let sum=0
For I in $ (seq 1 100); Do
If [$[$i%2] = = 0]; Then
Let sum+= $i
Fi
Done
echo "The sum is $sum."
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/8B/67/wKioL1hNdpDgF39uAAAfXIxnTlc966.png-wh_500x0-wm_3 -wmp_4-s_1860260142.png "style=" Float:none; "title=" 33.png "alt=" Wkiol1hndpdgf39uaaafxixntlc966.png-wh_50 "/>
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/8B/6A/wKiom1hNdpKBtzi1AAAG_tMSmpA261.png-wh_500x0-wm_3 -wmp_4-s_693087008.png "style=" Float:none; "title=" 34.png "alt=" Wkiom1hndpkbtzi1aaag_tmsmpa261.png-wh_50 "/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Shell scripting Exercises (12.11)