Vivasoft-logo

[⧍.ā§Ē] āĻĒā§āĻ°ā§āϝāĻžāĻ•āϟāĻŋāϏ āĻĒā§āϰāĻŦā§āϞ⧇āĻŽāϏ (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)
   }