Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views5 pages

Go Program Unit III

The document contains multiple Go programs demonstrating various functionalities such as finding minimum and maximum values in an array, calculating combinations using recursion, swapping two numbers, and performing string operations. Each program includes a main function that showcases the respective functionality with example outputs. The document serves as a practical guide for learning basic programming concepts in Go.

Uploaded by

fack20k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Go Program Unit III

The document contains multiple Go programs demonstrating various functionalities such as finding minimum and maximum values in an array, calculating combinations using recursion, swapping two numbers, and performing string operations. Each program includes a main function that showcases the respective functionality with example outputs. The document serves as a practical guide for learning basic programming concepts in Go.

Uploaded by

fack20k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

10.

program to find minimum and maximum using function

package main

import (
"fmt"
)

// Function to find minimum value


func findMin(arr []int) int {
min := arr[0]
for _, val := range arr {
if val < min {
min = val
}
}
return min
}

// Function to find maximum value


func findMax(arr []int) int {
max := arr[0]
for _, val := range arr {
if val > max {
max = val
}
}
return max
}

func main() {
numbers := []int{12, 45, 7, 89, 34, 22}

fmt.Println("Numbers:", numbers)
fmt.Println("Minimum number:", findMin(numbers))
fmt.Println("Maximum number:", findMax(numbers))
}

11. program to find nCr using recursive function

package main
import (
"fmt"
)

// Recursive function to calculate nCr


func nCr(n, r int) int {
if r == 0 || r == n {
return 1
}
return nCr(n-1, r-1) + nCr(n-1, r)
}

func main() {
n := 5
r := 2

fmt.Printf("n = %d, r = %d\n", n, r)


fmt.Printf("nCr = %d\n", nCr(n, r))
}

12. program to swap two numbers with a function returning two values

package main

import (
"fmt"
)

// Function to swap two numbers


func swap(a, b int) (int, int) {
return b, a
}

func main() {
x := 10
y := 20

fmt.Println("Before swapping:")
fmt.Println("x =", x, "y =", y)

// Call swap function


x, y = swap(x, y)
fmt.Println("After swapping:")
fmt.Println("x =", x, "y =", y)
}

13. program to demonstrate slices

package main

import (
"fmt"
)

func main() {
// Create an array
arr := [5]int{10, 20, 30, 40, 50}

// Create a slice from the array


slice1 := arr[1:4] // elements from index 1 to 3

fmt.Println("Array:", arr)
fmt.Println("Slice1 (arr[1:4]):", slice1)

// Append elements to slice


slice2 := append(slice1, 60, 70)
fmt.Println("Slice2 (after append):", slice2)

// Length and Capacity


fmt.Println("Length of Slice2:", len(slice2))
fmt.Println("Capacity of Slice2:", cap(slice2))

// Another slice directly


slice3 := []int{100, 200, 300}
fmt.Println("Slice3 (created directly):", slice3)
}

14. program for string operations

package main

import (
"fmt"
"strings"
)

func main() {
str1 := "Hello"
str2 := "World"

// Concatenation
result := str1 + " " + str2
fmt.Println("Concatenation:", result)

// Length of string
fmt.Println("Length of str1:", len(str1))

// Uppercase and Lowercase


fmt.Println("Uppercase:", strings.ToUpper(result))
fmt.Println("Lowercase:", strings.ToLower(result))

// Prefix and Suffix check


fmt.Println("Has prefix 'He':", strings.HasPrefix(str1, "He"))
fmt.Println("Has suffix 'ld':", strings.HasSuffix(str2, "ld"))

// Splitting
words := strings.Split(result, " ")
fmt.Println("Split into words:", words)

// Joining
joined := strings.Join(words, "-")
fmt.Println("Joined with '-':", joined)

// Contains substring
fmt.Println("Contains 'lo':", strings.Contains(str1, "lo"))
}

15. rogram to demonstrate string operations

package main

import (
"fmt"
"strings"
)

func main() {
str := " Go Programming Language "

// Trim spaces
trimmed := strings.TrimSpace(str)
fmt.Println("Original string:", str)
fmt.Println("After TrimSpace:", trimmed)

// Count occurrences
count := strings.Count(trimmed, "g")
fmt.Println("Count of 'g':", count)

// Index of substring
index := strings.Index(trimmed, "Program")
fmt.Println("Index of 'Program':", index)

// Compare strings
str1 := "GoLang"
str2 := "golang"
fmt.Println("Compare str1 and str2 (case-sensitive):", str1 == str2)
fmt.Println("Compare str1 and str2 (case-insensitive):",
strings.EqualFold(str1, str2))

// Repeat string
repeated := strings.Repeat("Go! ", 3)
fmt.Println("Repeated string:", repeated)

// Iterating over runes (characters)


fmt.Println("Characters in 'GoLang':")
for i, ch := range str1 {
fmt.Printf("Index %d: %c\n", i, ch)
}
}

You might also like