Skip to content

Commit

Permalink
more exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
avemuri57 committed Aug 15, 2018
1 parent 3d9d307 commit e104ff8
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 0 deletions.
13 changes: 13 additions & 0 deletions flow_ctrl/alt_for.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "fmt"

func main(){
sum := 1

for ; sum < 1000; {
sum +=sum
}

fmt.Println(sum)
}
20 changes: 20 additions & 0 deletions flow_ctrl/alt_if.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"math"
)


func pow(x, n, lim float64) float64{
if v := math.Pow(x,n); v < lim {
return v
}
return lim
}

func main(){

fmt.Println(pow(3,2,10))
fmt.Println(pow(3,3,10))
}
13 changes: 13 additions & 0 deletions flow_ctrl/defer-multi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "fmt"

func main(){
fmt.Println("Starting Program")

for i:=0;i<10;i++{
defer fmt.Println(i)
fmt.Println(i)
}

}
19 changes: 19 additions & 0 deletions flow_ctrl/for.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import "fmt"



func sumUp(n int) (sum int){
sum = 0
for i:=0;i<1+i;i++{
sum += i
}

return
}


func main(){
fmt.Println(sumUp(5))
}
18 changes: 18 additions & 0 deletions flow_ctrl/if.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import(
"fmt"
"math"
)

func sqrt(x float64) string{
if x<0{
return sqrt(-x) + "i"
}

return fmt.Sprint(math.Sqrt(x))
}

func main(){
fmt.Println(sqrt(2), sqrt(-4) )
}
23 changes: 23 additions & 0 deletions flow_ctrl/ifelse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
"math"
)

func pow(x, n, lim float64) float64{
if v:= math.Pow(x,n);v<lim{
return v
}else{
fmt.Printf("%g =< %g", lim, v)
}

return lim

}


func main(){
fmt.Println(pow(3,2,10))
fmt.Println(pow(3,3,20))
}
18 changes: 18 additions & 0 deletions flow_ctrl/while.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "fmt"


// For is Golang's while loop

func main(){
sum := 0

for sum < 1000 {
sum += sum
}

fmt.Println(sum)
}

// infinite for loop with for{}
19 changes: 19 additions & 0 deletions hello_world/basic-types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"math/cmplx"
)

var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5 + 12i)
)


func main(){
fmt.Printf("Type: %T Value: %v\n", ToBe,ToBe)
fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
fmt.Printf("Type: %T Value: %v\n", z,z)
}
20 changes: 20 additions & 0 deletions hello_world/numeric-constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "fmt"

const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
// Shift it right again 99 places, so we end up with 1<<1, or 2.
Small = Big >> 99
)

func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
return x * 0.1
}

func main() {
fmt.Printf(Big)
}
8 changes: 8 additions & 0 deletions hello_world/rand2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main
import(
"fmt"
"math/rand"
)
func main(){
fmt.Println("My favorite Number is", rand.Intn(20))
}
17 changes: 17 additions & 0 deletions hello_world/return-name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "fmt"



func split(sum int) (x,y,z int){
x = sum * 4/9
y = x * 5/8
z = x - y
return
}

func main(){
fmt.Println(split(40))
}

8 changes: 8 additions & 0 deletions hello_world/type-conv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "fmt"

func main(){
x := 42
fmt.Printf("v is of Type %T", v)
}
7 changes: 7 additions & 0 deletions hello_world/type-inf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main
import "fmt"

func main(){
v := 40
fmt.Printf("V has value %v and type %T\n",v,v)
}
14 changes: 14 additions & 0 deletions hello_world/variables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import "fmt"

var a, b, c bool

var k,x int = 1,2

func main(){

a := 4
fmt.Println(a)

}

0 comments on commit e104ff8

Please sign in to comment.