Created
February 26, 2017 08:16
-
-
Save rockos/30d2c9c0f79225588c52bd319588cc5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 1から100までの偶数の合計を算出する */ | |
package main | |
import "fmt" | |
func main() { | |
/* 変数 sum に合計を格納します */ | |
sum := 0 | |
/* for 文は命令を繰り返すときに使います */ | |
for i := 1; i <= 100; i++ { | |
/* 変数 i が 偶数かどうかを if 文で判定しています. */ | |
if i%2 == 0 { | |
sum += i | |
} | |
} | |
fmt.Printf("1から100までの偶数の合計 = %d \n", sum) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func fib_iter(n int) int { | |
if n < 2 { | |
return n | |
} | |
f1, f0 := 1, 0 | |
fn := f1 + f0 | |
for i := n; i >= 2; i-- { | |
fn = f1 + f0 | |
f1, f0 = fn, f1 | |
} | |
return fn | |
} | |
func main() { | |
for i:= 1; i <= 10; i++ { | |
fmt.Println(fib_iter(i)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func SumOfEven(a,b int) int { | |
sum := 0 | |
/* 配列から取りだした値を足し合計を表示する */ | |
for i := a; i <= b; i++ { | |
if i%2 == 0 { | |
sum += i | |
} | |
} | |
return sum | |
} | |
func main() { | |
/* 1から100までの偶数の合計を計算する */ | |
ans := SumOfEven(1, 100) | |
fmt.Printf("合計 = %d \n", ans) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func greeting(ampm string) { | |
var aisatu string | |
if ampm == "AM" { | |
aisatu = "Good morning." | |
} else { | |
aisatu = "Good afternoon." | |
} | |
for i:=0;i<3;i++ { | |
fmt.Println(aisatu) | |
} | |
} | |
func main() { | |
now := "PM" | |
greeting(now) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
kyori := 1500 | |
sokudo := 50 | |
jikan := kyori / sokudo | |
fmt.Printf("かかった時間は %d 分\n", jikan) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
/* 要素数 5 の配列を作る */ | |
aray := []int{60, 100, 80, 40, 50} | |
sum := 0 | |
/* 配列から取りだした値を足して合計を表示する */ | |
/* nは配列の番地.val はその番地に入っている値 */ | |
for n, val := range aray { | |
fmt.Printf("配列 %d の値は %d\n", n,val) | |
sum += val | |
} | |
fmt.Printf("合計= %d 点\n", sum) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment