-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (131 loc) · 2.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
)
func GCD(a int, b int) int {
var c int
for {
if a % b == 0 {
return b
}else{
c = b
b = a % b
a = c
}
}
}
func LCM(a int, b int) int {
c := GCD(a,b)
result := (a*b)/c
return result
}
func isPrime(a int) bool{
for i := 1; i<int(a/2);i++ {
if a % i == 0{
return false
}
}
return true
}
func palidrome(text string) bool{
temp := []rune(text)
for i, j := 0, len(temp)-1; i < j; i, j = i+1, j-1 {
temp[i],temp[j] = temp[j],temp[i]
}
if text == string(temp){
return true
}
return false
}
func selection(array []int) []int {
n := len(array)
for i := 0; i < len(array);i++{
index := i
for j := i + 1;j<n;j++{
if array[j]<array[index]{
index = j
}
}
temp := array[index]
array[index] = array[i]
array[i] = temp
}
return array
}
func bubble(array []int) []int {
n := len(array)
for i := 0; i<n;i++{
for j := 1; j<n-i;j++{
if array[j-1] > array[j] {
temp := array[j-1]
array[j-1] = array[j]
array[j] = temp
}
}
}
return array
}
func main(){
var option int
var a int
var b int
var word string
for {
fmt.Print("-------------------------\nChose an algorithm \n1.Greatest common divisor\n2.Least common multiple\n3.Prime number\n4.Is a palindrome\n5.Bubble sort\n6.Selection sort\nChoose option: ")
fmt.Scan(&option)
switch option {
case 1:
fmt.Print("Type numbers \n a =")
fmt.Scan(&a)
fmt.Print("b = ")
fmt.Scan(&b)
fmt.Print("GCD equals = ", GCD(a,b))
case 2:
fmt.Print("Type numbers \n a =")
fmt.Scan(&a)
fmt.Print("b = ")
fmt.Scan(&b)
fmt.Print("LCM equals = ",LCM(a,b))
case 3:
fmt.Print("Type numbers \n a =")
fmt.Scan(&a)
if isPrime(a) == true {
fmt.Print("Number is prime\n")
}else{
fmt.Print("Number is not prime\n")
}
case 4:
fmt.Print("Type a word: ")
fmt.Scan(&word)
if (palidrome(word)) {
fmt.Print("Word is a palindrome")
}else {
fmt.Print("Word is not a palindrome")
}
case 5:
fmt.Print("Create an array \n How long array should be = ")
fmt.Scan(&a)
array := []int{}
b = 0
for i := 0; i < a;i++ {
fmt.Print("\nAdd a number = ")
fmt.Scan(&b)
array = append(array, b)
}
fmt.Print("Sorted array = ", bubble(array),"\n")
case 6:
fmt.Print("Create an array \n How long array should be = ")
fmt.Scan(&a)
array := []int{}
b = 0
for i := 0; i < a;i++ {
fmt.Print("\nAdd a number = ")
fmt.Scan(&b)
array = append(array, b)
}
fmt.Print("Sorted array = ", selection(array),"\n")
default:
fmt.Print("Option does not exists\n")
}
}
}