Skip to content

Commit 5cafd65

Browse files
author
Rajeev Kumar Singh
committed
Code org
1 parent 1ac2e91 commit 5cafd65

File tree

52 files changed

+1001
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1001
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func avg(x float64, y float64) float64 {
6+
return (x + y) / 2
7+
}
8+
9+
func main() {
10+
x := 5.75
11+
y := 6.25
12+
13+
result := avg(x, y)
14+
15+
fmt.Printf("Average of %.2f and %.2f = %.2f\n", x, y, result)
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func getStockPriceChange(prevPrice, currentPrice float64) (float64, float64) {
9+
change := currentPrice - prevPrice
10+
percentChange := (change / prevPrice) * 100
11+
return change, percentChange
12+
}
13+
14+
func main() {
15+
prevStockPrice := 0.0
16+
currentStockPrice := 100000.0
17+
18+
change, percentChange := getStockPriceChange(prevStockPrice, currentStockPrice)
19+
20+
if change < 0 {
21+
fmt.Printf("The Stock Price decreased by $%.2f which is %.2f%% of the prev price\n", math.Abs(change), math.Abs(percentChange))
22+
} else {
23+
fmt.Printf("The Stock Price increased by $%.2f which is %.2f%% of the prev price\n", change, percentChange)
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"math"
7+
)
8+
9+
func getStockPriceChangeWithError(prevPrice, currentPrice float64) (float64, float64, error) {
10+
if prevPrice == 0 {
11+
err := errors.New("Previous price cannot be zero")
12+
return 0, 0, err
13+
}
14+
change := currentPrice - prevPrice
15+
percentChange := (change / prevPrice) * 100
16+
return change, percentChange, nil
17+
}
18+
19+
func main() {
20+
prevStockPrice := 0.0
21+
currentStockPrice := 100000.0
22+
23+
change, percentChange, err := getStockPriceChangeWithError(prevStockPrice, currentStockPrice)
24+
25+
if err != nil {
26+
fmt.Println("Sorry! There was an error: ", err)
27+
} else {
28+
if change < 0 {
29+
fmt.Printf("The Stock Price decreased by $%.2f which is %.2f%% of the prev price\n", math.Abs(change), math.Abs(percentChange))
30+
} else {
31+
fmt.Printf("The Stock Price increased by $%.2f which is %.2f%% of the prev price\n", change, percentChange)
32+
}
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func getNamedStockPriceChange(prevPrice, currentPrice float64) (change, percentChange float64) {
9+
change = currentPrice - prevPrice
10+
percentChange = (change / prevPrice) * 100
11+
return change, percentChange
12+
}
13+
14+
func main() {
15+
prevStockPrice := 100000.0
16+
currentStockPrice := 90000.0
17+
18+
change, percentChange := getNamedStockPriceChange(prevStockPrice, currentStockPrice)
19+
20+
if change < 0 {
21+
fmt.Printf("The Stock Price decreased by $%.2f which is %.2f%% of the prev price\n", math.Abs(change), math.Abs(percentChange))
22+
} else {
23+
fmt.Printf("The Stock Price increased by $%.2f which is %.2f%% of the prev price\n", change, percentChange)
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
func getStockPriceChange(prevPrice, currentPrice float64) (float64, float64) {
9+
change := currentPrice - prevPrice
10+
percentChange := (change / prevPrice) * 100
11+
return change, percentChange
12+
}
13+
14+
func main() {
15+
prevStockPrice := 80000.0
16+
currentStockPrice := 120000.0
17+
18+
change, _ := getStockPriceChange(prevStockPrice, currentStockPrice)
19+
20+
if change < 0 {
21+
fmt.Printf("The Stock Price decreased by $%.2f\n", math.Abs(change))
22+
} else {
23+
fmt.Printf("The Stock Price increased by $%.2f\n", change)
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var x [5]int // An array of 5 integers
7+
fmt.Println(x)
8+
9+
var y [8]string // An array of 8 strings
10+
fmt.Println(y)
11+
12+
var z [3]complex128 // An array of 3 complex numbers
13+
fmt.Println(z)
14+
15+
// By default, all the array elements are assigned the zero value of the array type.
16+
// For example, if we declare an integer array, all the elements will be initialized with zero.
17+
// If we declare a string array, all the elements will be initialized with an empty string, and so on.
18+
}

07-arrays/02-array-indexing/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var x [5]int // An array of 5 integers
7+
8+
x[0] = 100
9+
x[1] = 101
10+
x[3] = 103
11+
x[4] = 105
12+
13+
fmt.Printf("x[0] = %d, x[1] = %d, x[2] = %d\n", x[0], x[1], x[2])
14+
fmt.Println("x = ", x)
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Declaring and initializing an array at the same time
7+
var a = [5]int{2, 4, 6, 8, 10}
8+
fmt.Println(a)
9+
10+
// Short hand declaration for declaring and initializing an array
11+
b := [5]int{2, 4, 6, 8, 10}
12+
fmt.Println(b)
13+
14+
// You don't need to initialize all the elements of the array.
15+
// The un-initialized elements will be assigned the zero value of the corresponding array type
16+
c := [5]int{2}
17+
fmt.Println(c)
18+
19+
// Letting Go compiler infer the length of the array
20+
d := [...]int{3, 5, 7, 9, 11, 13, 17}
21+
fmt.Println(d)
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
a1 := [5]string{"English", "Japanese", "Spanish", "French", "Hindi"}
7+
a2 := a1 // A copy of the array `a1` is assigned to `a2`
8+
9+
a2[1] = "German"
10+
11+
fmt.Println("a1 = ", a1) // The array `a1` remains unchanged
12+
fmt.Println("a2 = ", a2)
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Iterating over and Array and printing its elements
7+
names := [3]string{"Mark Zuckerberg", "Bill Gates", "Larrt Page"}
8+
9+
for i := 0; i < len(names); i++ {
10+
fmt.Println(names[i])
11+
}
12+
13+
// Finding the Sum of an Array
14+
a := [4]float64{3.5, 7.2, 4.8, 9.5}
15+
sum := float64(0)
16+
17+
for i := 0; i < len(a); i++ {
18+
sum = sum + a[i]
19+
}
20+
21+
fmt.Printf("Sum of all the elements in array %v = %f\n", a, sum)
22+
}

0 commit comments

Comments
 (0)