OneCompiler

GO.RA

304

1**. //Write a program in GO language to accept user choice and print answers using arithmetic operators.**
package main
import "fmt"
func main(){
var a,b,z int

fmt.Printf("Enter Two Numbers :")
fmt.Scanf("%d%d",&a,&b)

fmt.Printf("\n 1. Addition \n 2. Substraction \n 3. Multiplication \n 4. Division \n 5. Remainder")

fmt.Printf("\n Enter Your Choice :")
fmt.Scanf("%d",&z)

switch z{
case 1:
fmt.Printf("\n Addition = %d",a+b
case 2:
fmt.Printf("\n Substraction=%d",a-b)
case 3:
fmt.Printf("\n Multiplication= %d",a*b)
case 4:
fmt.Printf("\n Division = %d",a/b)
case 5:
fmt.Printf("\n Remainder = %d",a%b)
default:
fmt.Printf("\n Wrong Choice.")
}

}

2**.//Write a program in GO language to print Fibonacci series of n terms.**
package main
import "fmt"
func fibo(n int){
a,b:=0,1
for i:=0;i<n;i++{ fmt.Println(a)
a,b=b,a+b
}

}

func main(){
var n int
fmt.Println("Enter the term :") fmt.Scanf("%d",&n)
fibo(n)
}

3.//Write a program in GO language using function to check whether accepts number is palindrome or not.

package main
import. "fmt"
func check(n int)string{
store:=n
sum:=0
var rem int
for (n>0){
rem=n%10
sum=(sum*10)+rem
n=n/10
}
if (store==sum){
return "Palindrome
}else{
return "not"
}
}

func main(){
Println(check(101))
}

4**.// Write a program in GO language to print a recursive sum of digits of a given number.**

package main
import "fmt"
func sumofdigits(num int) int{ if(num==0){
return 0

}else{
return(num%10)+sumofdigits(num/10)
}

}
func main(){ fmt.Println(sumofdigits(120))

}

5**.// Write a program in GO language program to create Text file**

package main
import(
"log"
"os"
)

func main(){ emptyFile,err:=os.Create("Empty.txt") if err!=nil{
log.Fatal(err)
}
log.Println(emptyFile) emptyFile.Close()
}

6**.// Write a program in GO language to copy all elements of one array into another using a method.**

package main
import "fmt"
func main(){
Array1:=[]int{1,2,3,4,5} Array2:=make([]int, len(Array1)) copy(Array2,Array1) fmt.Println("Array2:",Array2)
}

7**.//Write a program in GO language to create structure student. Write a method show() whose receiver is a pointer of struct student.**

package main
import. "fmt"
type student struct{
name string
age int
gender string

}

func(s *student) show(){ Println("Name:",s.name) Println("Age:",s.age) Println("gender:",s.gender)
}

func main(){
var s student
Println("Enter student details :") Print("Name:")
Scanln(&s.name)
Print("Age:")
Scanln(&s.age)
Print("gender:") Scanln(&s.gender)
s.show()
}

8**.// Write a program in GO language to accept the book details such as BookID, Title, Author, Price. Read and display the details of ā€˜n’ number of books**

package main
import. "fmt"
Type Books struct{
book_id int
title string
author string
price float32
}

func main(){
var n int
Print("Enter number of books :") Scanf("%d",&n) bk:=make([]Books,n) Println("Enter Book details :") for i:=0;i<n;i++{
Print("Enter Book Id:") Scanln(&bk[i].book_id) Print("Enter Book title :") Scanln(&bk[i].title) Println("Enter author details:") Scanln(&bk[i].author) Print("Enter book price:") Scanln(&bk[i].price)
}
for i:=0;i<n;i++{

	Printf("\n Book Id : %d Title : %s Author : %s price: %f", bk[i].book_id,bk[i].title,bk[i].author,bk[i].price) 
	} 

}

9**.//Write a program in GO language using function to check whether the accepted number is pa**lindrome or not.

package main
import. "fmt"
func check(n int)string{
store:=n
sum:=0
var rem int
for (n>0){
rem=n%10 sum=(sum*10)+rem
n=n/10
}
if (store==sum){
return "Palindrome"
}else{
return "not"
}

}

func main(){
Println(check(101))
}

10.// Write a program in GO language to create an interface and display its values with the help of type assertion.

package main
import. "fmt"

func main(){ 

var myInt interface{}=123 k,ok:=myInt.(int)
if ok{
Println("Sucesss :",k)
}
v,ok:=myInt.(float64)
if ok{
Println(v)
}else{
Println("Failed without panicking.")

}
}

11.// Write a program in GO language to check whether the accepted number is two digit or not.

package main
import. "fmt"
func main(){
var num int
Println("Enter the number :") Scanf("%d",&num)
if num>=10 && num<=99{ Println("The number is two digit .")
}else{
Println("The number is not two digit.")
}

}

12.//Write a program in GO language to swap two numbers using call by reference concept

package main
import. "fmt"
func swap(xint,yint){
var temp int
temp=*x
*x=*y
*y=temp
}
func main(){
var a,b int
Println("Enter the two numbers :")
Scanf("%d%d",&a,&b) Printf("Before swapping the number is : a=%d and b=%d",a,b) swap(&a,&b)
Printf("After swapping the number is : a=%d and b=%d",a,b)
}

13.//Write a program in GO language to print sum of all even and odd numbers separately between 1 to 100.

package main
import "fmt"
func main(){
sumEven:=0
sumOdd:=0
for i:=1;i<=100;i++{
if i%2==0{
sumEven+=i
}else{
sumOdd+=i
}

}
fmt.Println("Sum of even numbers between 1 to 100:",sumEven) fmt.Println("Sum of odd numbers between 1 to 100:",sumOdd)
}

14.//Write a program in GO language to demonstrate working of slices (like append, remove, copy etc.)

package main
import. "fmt"
func main(){

Array1:=[]int{1,2,3,4,5,6} Println("Original slice : ", Array1)

Array1=append(Array1,7,8,9) Println("After append :",Array1)

Remove:=2 Array1=removeElement(Array1,Remove) Println("After Remove",Remove)

Array2:=make([]int,len(Array1)) copy(Array2,Array1) Println("Copied Array:",Array2)
}
func removeElement(Array1 []int,index int)[]int{
return append(Array1[:index],Array1[index+1:]...)

}

15.// Write a program in GO language to demonstrate function return multiple values.

package main
import "fmt"
func swap(x,y int)(int,int){
return y,x

}
func main(){
a,b:=swap(10,20) fmt.Println("Original Values :",10,20)
fmt.Println("Swapped Values :",a,b)

}

16.// Write a program in GO language that prints out the numbers from 0 to 10, waiting between 0 and 250 ms after each one using the delay function.

package main
import(
"fmt"
"time"
)

func numbers(){
for i:=1;i<=5;i++{ time.Sleep(250 * time.Millisecond) fmt.Printf("%d",i)
}

}

func alphabets(){
for i:='a';i<='e';i++{ time.Sleep(400 * time.Millisecond) fmt.Printf("%c",i)
}
}

func main(){
go numbers()
go alphabets()
time.Sleep(3000 * time.Millisecond) fmt.Printf("Main Terminated")

}

17.// Write a program in GO language to illustrate the concept of returning multiple values from a function. ( Add, Subtract, Multiply, Divide)

package main
import. "fmt"

func add(a,b float64) float64{ return a+b

}
func sub(a,b float64) float64{ return a-b

}
func mult(a,b float64) float64{ return a*b
}
func div(a,b float64) float64{ return a/b
}

func main(){
var n1,n2 float64
Print("Enter the first number :") Scanln(&n1)
Print("Enter the second number :") Scanln(&n2)

Println("Addition is :",add(n1,n2)) Println("Subtraction is :",sub(n1,n2)) Println("multiplication is :",mult(n1,n2))
Println("division is :",div(n1,n2))

}

18.// Write a program in GO language to print a multiplication table of number using function.

package main
import. "fmt"
func multi(num int){ Printf("multiplication Table of : %d \n",num)
for i:=1;i<=10;i++{ Printf("%dx%d=%d\n",num,i,num*i)

}

}

func main(){
var num int
Printf("Enter the number :") Scanln(&num)
multi(num)

}

19.// Write a program in GO language to illustrate the function returning multiple values(add, subtract).

package main
import. "fmt"
func addSub(a,b int)(int,int){ sum:=a+b
diff:=a-b
return sum,diff

}

func main(){
var x,y int
Print("Enter the number :") Scanf("%d%d",&x,&y) resultSum,resultDiff:=addSub(x,y) Println("Sum : %d",resultSum) Println("diff:%d",resultDiff)

}

20.//Write a program in Go language how to create a channel and illustrate how to close a channel using for range loop and close function.

package main
import. "fmt"

func sender(ch chan int){
defer close(ch)
for i:=1;i<=5;i++{
ch<-1
}

}

func main(){
ch:=make(chan int)
go sender(ch)
for num:=range ch{ Println("Recieved :",num)
}

}