[⧍.ā§Ē] āĻĒā§āϰā§āϝāĻžāĻāĻāĻŋāϏ āĻĒā§āϰāĻŦā§āϞā§āĻŽāϏ (Practice Problems)
1. āĻāĻāĻāĻŋ āĻĢāĻžāĻāĻļāύ āϞāĻŋāĻā§āύ āϝā§āĻāĻŋ āĻĻā§āĻāĻŋ āϏāĻāĻā§āϝāĻž āĻĒā§āϝāĻžāϰāĻžāĻŽāĻŋāĻāĻžāϰ āĻšāĻŋāϏā§āĻŦā§ āύā§ā§ , āϏāĻāĻā§āϝāĻž āĻĻā§āĻāĻŋ āϝā§āĻ āĻāϰ⧠āĻāĻŦāĻ āϝā§āĻāĻĢāϞ return āĻāϰ⧠–Â
package main
import "fmt"
// implement the function sum(x,y) here
func main() {
x, y := 10, 20
result := sum(x, y)
fmt.Printf("The sum of %d and %d is %d\n", x, y, result)
}
// Output : The sum of 10 and 20 is 30
2. āĻāĻāĻāĻŋ āĻĢāĻžāĻāĻļāύ āϞāĻŋāĻā§āύ āϝā§āĻāĻŋ āĻŦāĻŋāĻāĻŋāύā§āύ āϏāĻāĻā§āϝāĻ āĻāĻāĻ āĻĄāĻžāĻāĻž āĻāĻžāĻāĻĒā§āϰ āĻāύāĻĒā§āĻ āĻāϰā§āĻā§āĻŽā§āύā§āĻ āĻšāĻŋāϏā§āĻŦā§ āĻā§āϰāĻšāĻŖ āĻāϰāϤ⧠āĻĒāĻžāϰāĻŦā§ , āϏāĻāĻā§āϝāĻž āĻā§āϞ⧠āϝā§āĻ āĻāϰ⧠āϝā§āĻāĻĢāϞ return āĻāϰāĻŦā§ –Â
package main
import "fmt"
// implement the function sumAll(arg... int) here
func main() {
arr1 := []int{1, 2, 3}
arr2 := []int{1, 2, 3, 4}
arr3 := []int{1, 2, 3, 4, 5}
result1 := sumAll(arr1...)
result2 := sumAll(arr2...)
result3 := sumAll(arr3...)
fmt.Println("sum of first array ", result1)
fmt.Println("sum of second array ", result2)
fmt.Println("sum of third array ", result3)
}
// Output :
// sum of first array 6
// sum of first array 10
// sum of first array 15
3. āĻāĻāĻāĻŋ āĻĢāĻžāĻāĻļāύ āϞāĻŋāĻā§āύ āϝā§āĻāĻŋ āĻāĻāĻāĻŋ āĻĢāĻžāĻāϞ create āĻāϰāĻŦā§ āĻāĻŦāĻ āĻĢāĻžāĻāϞāĻāĻŋāϤ⧠“Hello World” āϞāĻŋāĻāĻžāϰ āĻĒāϰ defer āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻĢāĻžāĻāϞāĻāĻŋ close āĻāϰ⧠āĻĻāĻŋāĻŦā§ –
package main
import (
"fmt"
"io/ioutil"
"log"
)
func main() {
// Create a file with file name example.txt
// close file
// Write something to example.txt
content, err := ioutil.ReadFile("example.txt")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(content))
}
// Output : Hello, World!
4. āĻāĻāĻāĻŋ āĻĢāĻžāĻāĻļāύ āϞāĻŋāĻā§āύ āϝā§āĻāĻŋ āĻāĻāĻāĻŋ āϏāĻāĻā§āϝāĻž āĻĒā§āϝāĻžāϰāĻžāĻŽāĻŋāĻāĻžāϰ āĻšāĻŋāϏā§āĻŦā§ āύāĻŋāĻŦā§ āĻāĻŦāĻ āϏāĻāĻā§āϝāĻžāĻāĻŋ āϧāύāĻžāϤā§āĻŽāĻ āĻšāϞ⧠āϏāĻāĻā§āϝāĻžāĻāĻŋāϰ square root āĻāĻŦāĻ positive āĻŽā§āϏā§āĻ return āĻāϰāĻŦā§ , āĻ āύā§āϝāĻĨāĻžā§ -1 āĻāĻŦāĻ negative āĻŽā§āϏā§āĻ return āĻāϰāĻŦā§ –
package main
import (
"fmt"
)
// implement check(n) function here
func main() {
var n int
fmt.Scan(&n)
result, message := check(n)
if message == "negative" {
fmt.Println("this is a negative number, So square root of ", n, " is impossible")
} else {
fmt.Println("square root of ", n, " is ", result)
}
}
// Output :
// 16
// square root of 16 is 4"
5. āĻāĻāĻāĻŋ āĻĢāĻžāĻāĻļāύ āϞāĻŋāĻā§āύ, āϝāĻž āĻāĻāĻāĻŋ String āϰāĻŋāϏāĻŋāĻ āĻāϰāĻŦā§ āĻāĻŦāĻ āĻāĻāĻāĻŋ Boolean āϰāĻŋāĻāĻžāϰā§āύ āĻāϰāĻŦā§āĨ¤ āϝāĻĻāĻŋ String āĻāϰ āϏāĻāϞ āĻā§āϝāĻžāϰā§āĻā§āĻāĻžāϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āĻĒā§āϞāĻŋāύāĻĄā§āϰāĻŽ āĻŦāĻžāύāĻžāύ⧠āϏāĻŽā§āĻāĻŦ āĻšā§ āϤāĻžāĻšāϞ⧠true, āĻāϰ āύā§āϤ⧠false āϰāĻŋāĻāĻžāϰā§āύ āĻāϰāĻŦā§āĨ¤
package main
import (
"fmt"
)
func isPalindromePossible(str string) bool
func main() {
str := "madam"
if isPalindromePossible(str) {
fmt.Printf("%q is a palindrome\n", str)
} else {
fmt.Printf("%q is not a palindrome\n", str)
}
}
6. āĻāĻāĻāĻŋ āĻĢāĻžāĻāĻļāύ āϞāĻŋāĻā§āύ, āϝāĻž āĻāĻāĻāĻŋ āĻāύā§āĻāĻŋāĻāĻžāϰā§āϰ āϏā§āϞāĻžāĻāϏ āϰāĻŋāϏāĻŋāĻ āĻāϰāĻŦā§ āĻāĻŦāĻ āĻāϰ āϏāĻāĻā§āϝāĻžāĻā§āϞā§āϰ āĻā§ āϰāĻŋāĻāĻžāϰā§āύ āĻāϰāĻŦā§āĨ¤
package main
import "fmt"
func average(numbers []int) float64
func main() {
numbers := []int{1, 2, 3, 4, 5}
avg := average(numbers)
fmt.Printf("The average of %v is %.2f\n", numbers, avg)
}