0% found this document useful (0 votes)
17 views16 pages

Go Programs for LCM, GCD, and More

The document contains a series of Go programming experiments, each demonstrating different functionalities such as calculating LCM and GCD, printing pyramids, using structs from packages, calculating standard deviation, and building a contact form. Each experiment includes code snippets, input examples, and expected outputs. The document serves as a comprehensive guide for learning various programming concepts in Go.

Uploaded by

thungarenuka80
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)
17 views16 pages

Go Programs for LCM, GCD, and More

The document contains a series of Go programming experiments, each demonstrating different functionalities such as calculating LCM and GCD, printing pyramids, using structs from packages, calculating standard deviation, and building a contact form. Each experiment includes code snippets, input examples, and expected outputs. The document serves as a comprehensive guide for learning various programming concepts in Go.

Uploaded by

thungarenuka80
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

Program

Program Write a Go Program to find LCM and GCDof given two numbers.
code package main
import "fmt"

func gcd(a, b int) int {


for b != 0 {
a, b = b, a % b
}
return a
}

func lcm(a, b int) int {


return (a * b) / gcd(a, b)
}

func main() {
var a, b int
[Link]("Enter two numbers: ")
[Link](&a, &b)

result := lcm(a, b)
[Link]("The LCM of %d and %d is %d\n", a, b, result)
}
Run go run [Link]
Input 36
Output The LCM of 3 and 6 is 6
Experiment2
Program Write a GO Program to print Pyramid of numbers.
code package main
import "fmt"

func main() {
var n int
[Link]("Enter the number of levels: ")
[Link](&n)

for i := 1; i <= n; i++ {


// Print leading spaces
for j := 1; j <= n-i; j++ {
[Link](" ")
}

// Print numbers in increasing order


for j := 1; j <= i; j++ {
[Link](j)
}

// Print numbers in decreasing order


for j := i - 1; j >= 1; j-- {
[Link](j)
}

// Move to the next line


[Link]()
}
}
Run go run [Link]
Input Enter the number of levels: 3
Output 1
121
12321
C:\Users\student\Desktop\New folder>
Experiment3
Program Write a program to use struct that is imported from another package.
File [Link]
name:
code package main
import "fmt"
import "newpack"
func main() {
result := [Link](5)
[Link]("Square of 5 is:", result)
}
Location any folder
Run go run [Link]
File [Link]
name:
package newpack
func Square(x int) int {
return x * x
}
Location C:\Program Files (x86)\Go\src\newpack\ [Link]
Output Square of 5 is: 25
Experiment4
Program Write a program to use struct that is imported from another package.
File [Link]
name:
code package main
import "fmt"
import "newpack"
func main() {
result := [Link](5)
[Link]("Square of 5 is:", result)
}
Location any folder
Run go run [Link]
File [Link]
name:
package newpack
func Square(x int) int {
return x * x
}
Location C:\Program Files (x86)\Go\src\newpack\ [Link]
Output Square of 5 is: 25
Experiment5
Program Write a GOProgram to calculate Standard deviation in math package
code package main
import "fmt"
import "math"

func main(){
var num[10] float64
var sum,mean,sd float64

[Link]("****** Enter 10 elements *******")


for i := 1; i <= 10; i++ {
[Link]("Enter %d element : ",i)
[Link](&num[i-1])
sum += num[i-1]
}

mean = sum/10;

for j := 0; j < 10; j++ {


sd += [Link](num[j]- mean, 2)
}

sd = [Link](sd/10)
[Link]("The Standard Deviation is : ",sd)
}
Input ****** Enter 10 elements *******
Enter 1 element : 3
Enter 2 element : 5
Enter 3 element : 9
Enter 4 element : 1
Enter 5 element : 8
Enter 6 element : 6
Enter 7 element : 58
Enter 8 element : 9
Enter 9 element : 4
Enter 10 element : 10
Output The Standard Deviation is : 15.8117045254457
Experiment5
Program Write a program in GO language to print Floyd’s Triangle.
code package main
import "fmt"
func main(){
var rows int
var temp int = 1

[Link]("Enter number of rows : ")


[Link](&rows)
for i := 1; i <= rows; i++ {
for k := 1; k <= i; k++ {
[Link](" %d",temp)
temp++
}
[Link]("")
}
}
Input Enter number of rows : 5
Output 1
23
456
7 8 9 10
11 12 13 14 15

Experiment6
Program Write a GOProgram to take user input and addition of two strings.
code package main
import "fmt"
func main() {
[Link]("Enter First String: ")
var first string
[Link](&first)

[Link]("Enter Second String: ")


var second string
[Link](&second)

[Link]("concatenation of two strings: ",first + second)


}
Input Enter First String: a
Enter Second String: b
Run go run [Link]
Output concatenation of two strings: ab

Experiment7
Program Write a GOProgram to check whether a string is Palindrome or not.
code package main
import "fmt"

func main() {
[Link]("palindrome program checker ")
var number,temp, reverse,rem int = 45454, 0, 0, 0

temp=number
for{
rem = number%10
reverse = reverse*10 + rem
number /= 10
if(number==0){
break
}
}
if(temp==reverse){
[Link]("Number %d is a Palindrome",temp)
}else{
[Link]("Number %d is not a Palindrome",temp)
}
}
Input Number 45454
Run go run [Link]
Output palindrome program checker
Number 45454 is a Palindrome
Experiment8
Program Write a GO Program to Build a Contact form
File Name [Link]
code <!DOCTYPE html>
<html>
<head>
<title>Simple Form Example</title>
</head>
<body>
<h2>Submit Form</h2>
<form action="/" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>

<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>

<input type="reset" value="reset">


<input type="submit" value="Submit">
</form>
</body>
</html>
File Name [Link]
code package main
import (
"fmt"
"net/http"
)

func main() {
[Link]("/", func(w [Link], r *[Link]) {
if [Link] != [Link] {
// Serve the HTML form on GET request
[Link](w, r, "[Link]")
return
}

// Parse form data


err := [Link]()
if err != nil {
[Link](w, "Error parsing form data", [Link])
return
}
// Extract form data
name := [Link]("name")
email := [Link]("email")

// Print the form data


[Link]("Name: %s\n", name)
[Link]("Email: %s\n", email)

// Respond with a confirmation message


[Link](w, "Received form submission:\nName: %s\nEmail: %s\n", name,
email)
})

[Link]("Server is running on localhost:8080")


[Link](":8080", nil)
}
Input
Run go run [Link]
Output
Experiment8
Program Write a GO Program to Build a Contact form
File Name [Link]
code <!DOCTYPE html>
<html>
<head>
<title>Simple Form Example</title>
</head>
<body>
<h2>Submit Form</h2>
<form action="/" method="post">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" required><br><br>

<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>

<label for="email">Message:</label><br>
<input type="Message" id="Message" name="Message" required><br><br>

<input type="reset" value="reset">


<input type="submit" value="Submit">
</form>
</body>
</html>
File Name [Link]
code package main
import "fmt"
import "net/http"

func main() {
[Link]("/", func(w [Link], r *[Link]) {
if [Link] != [Link] {
// Serve the HTML form on GET request
[Link](w, r, "[Link]")
return
}

// Parse form data


err := [Link]()
if err != nil {
[Link](w, "Error parsing form data", [Link])
return
}

// Extract form data


name := [Link]("name")
email := [Link]("email")
msg := [Link]("Message")

// Print form data on console


[Link]("Name1: %s\n", name)
[Link]("Email1: %s\n", email)
[Link]("msg1: %s\n", msg)

// Respond with a confirmation message on browser


[Link](w, "Received form submission:\nName: %s\nEmail: %s \
nMessage: %s\n", name, email, msg)
})

[Link]("Server is running on localhost:8080")


[Link](":8080", nil)
}
Input

Run go run [Link]


Output
Experiment9
Program Write a GOProgram to calculate average using arrays
File Name [Link]
code package main
import "fmt"

func main(){
var num[100] int
var temp,sum,avg int

[Link]("Enter number of elements: ")


[Link](&temp)

for i := 0; i < temp; i++ {


[Link]("Enter the number : ")
[Link](&num[i])
sum += num[i]
}

avg = sum/temp
[Link]("Average of entered %d number(s) is %d",temp,avg)
}
Input Enter number of elements: 5
Enter the number : 1
Enter the number : 2
Enter the number : 3
Enter the number : 4
Enter the number : 5
Run go run [Link]
Output Average of entered 5 number(s) is 3
Experiment10
Program Write a GOProgram to delete duplicate element in a given array
File Name [Link]
code package main
import "fmt"

func removeDuplicates(arr []int) []int {


encountered := map[int]bool{} // Track encountered elements
result := []int{} // Result slice without duplicates

for _, v := range arr {


if !encountered[v] {
encountered[v] = true
result = append(result, v)
}
}
return result
}

func main() {
arr := []int{1, 2, 3, 2, 4, 5, 1, 6, 3}
[Link]("Original Array:", arr)

arr = removeDuplicates(arr)
[Link]("Array after removing duplicates:", arr)
}
Input Original Array: [1 2 3 2 4 5 1 6 3]
Run go run [Link]
Output Array after removing duplicates: [1 2 3 4 5 6]
Experiment11
Program Write a GOProgram with an example of Array reverse sort Functions for integers and
strings.
File Name [Link]
code package main
import "fmt"
import "sort"

func main() {
[Link]("Interger Reverse Sort")
num := []int{50,90, 30, 10, 50}
[Link]([Link]([Link](num)))
[Link](num)
[Link]()

[Link]("String Reverse Sort")


text := []string{"Japan","UK","Germany","Australia","Pakistan"}
[Link]([Link]([Link](text)))
[Link](text)
}
Input Interger Reverse Sort
[50,90, 30, 10, 50]

String Reverse Sort


{"Japan","UK","Germany","Australia","Pakistan"}
Run go run [Link]
Output Interger Reverse Sort
[90 50 50 30 10]

String Reverse Sort


[UK Pakistan Japan Germany Australia]
Experiment12
Program Write a program comprising of Contains , Contains Any , Count and Equal Fold String
functions.
File Name [Link]
code package main
import "fmt"
import "strings"

func main() {
[Link]([Link]("Germany", "G"))
[Link]([Link]("Germany", "g"))
[Link]([Link]("Germany", "Ger"))
[Link]([Link]("Germany", "ger"))
[Link]([Link]("Germany", "er"))
[Link]([Link]("cheese", "e"))
[Link]([Link]("Cat", "cAt"))
[Link]([Link]("India", "Indiana"))
}
Input
Run go run [Link]
Output true
false
true
false
true
3
true
false
Experiment13
Program Write a GOProgram for CRUD using MYSQL fromscratch
File Name [Link]
code
Input
Run go run [Link]
Output

You might also like