Go Programming Articles

Page 5 of 86

Golang Program to find the parity of a given number.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 1K+ Views

Definition − Parity refers to the count of 1s. If count of 1s is even, then it’s even parity; and if the count of 1s is odd, then the parity is Odd.ExamplesConsider n = 20(00010100)Parity of the given number 20 is even.Approach to solve this problemStep 1 − Define a method, where n and is an argument, return type is int.Step 2 − Calculate the count of 1s in the given number’s bit.Examplepackage main import (    "fmt"    "strconv" ) func FindParity(n int) bool {    parity := false    for n != 0 {       if ...

Read More

Golang Program to count the number of flips to convert a given integer to another.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 217 Views

ExamplesConsider two numbers m = 65 => 01000001 and n = 80 => 01010000Number of bits flipped is 2.Approach to solve this problemStep 1 − Convert both numbers into bits.Step 2 − Count number of bits are flipped.Examplepackage main import (    "fmt"    "strconv" ) func FindBits(x, y int) int{    n := x ^ y    count := 0    for ;n!=0; count++{       n = n & (n-1)    }    return count } func main(){    x := 65    y := 80    fmt.Printf("Binary of %d is: %s.", x, strconv.FormatInt(int64(x), 2))    fmt.Printf("Binary ...

Read More

Golang Program to check if the binary representation of a number is palindrome or not

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 326 Views

ExamplesFor example, 101, 11, 11011, 1001001 are Palindrome. 100, 10010 are not Palindrome.Approach to solve this problemStep 1 − Convert number into binary representation.Step 2 − Traverse the converted binary representation from both side and check whether representation is palindrome or not.Examplepackage main import (    "fmt"    "strconv" ) func IsPalindrome(n int) bool{    rev := 0    k := n    for k != 0 {       rev = (rev > 1    }    return n == rev } func main(){    n := 3    fmt.Printf("Binary representation of %d is: %s.", n,    strconv.FormatInt(int64(n), 2))    if IsPalindrome(n) == true{       fmt.Println("Palindrome")    } else {       fmt.Println("Not a Palindrome")    } }OutputBinary representation of 3 is: 11. Palindrome

Read More

Golang Program to count the set bits in an integer.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 2K+ Views

ExamplesFor example, 101, 11, 11011 and 1001001 set bits count 2, 2, 4 and 3 respectively.Approach to solve this problemStep 1 − Convert number into binary representation.Step 2 − Count the number of 1s; return count.Examplepackage main import (    "fmt"    "strconv" ) func NumOfSetBits(n int) int{    count := 0    for n !=0{       count += n &1       n >>= 1    }    return count } func main(){    n := 20    fmt.Printf("Binary representation of %d is: %s.", n,    strconv.FormatInt(int64(n), 2))    fmt.Printf("The total number of set bits in %d is %d.", n, NumOfSetBits(n)) }OutputBinary representation of 20 is: 10100. The total number of set bits in 20 is 2.

Read More

Golang Program to find the minimum and maximum number, using binary operations.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 286 Views

ExamplesFor example, x = 12, y = 15 => Maximum number is 15.For example, x = 13, y = 17 => Minimum number is 13.Approach to solve this problemStep 1 − Define method, findMax and findMin, that accept two integer x and y.Step 2 − Return integer according to defined method.Examplepackage main import "fmt" func FindMax(x, y int){    fmt.Printf("Maximum element in %d, and %d is: %d", x, y, x - ((x - y) &    ((x - y) >> 31))) } func FindMin(x, y int) {    fmt.Printf("Minimum element in %d, and %d is: %d", x, y, y + ...

Read More

Golang Program to round up the next highest power of 2.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 631 Views

ExamplesFor example, n = 12 => Next number power of 2 is 16.For example, n = 20 => Next number power of 2 is 32.Approach to solve this problemStep 1 − Define method, that accepts a number n.Step 2 − Iterate k := 1 until k < n.Step 3 − In a loop, calculate k

Read More

Golang Program to round up the next previous power of 2.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 306 Views

ExamplesFor example, n = 12 => Previous number power of 2 is 8.For example, n = 20 => Previous number power of 2 is 16.Approach to solve this problemStep 1 − Define a method that accepts a number n.Step 2 − Perform n | (n >> k), where k is 1, 2, 4, 8, 16.Step 3 − At the end, return n - (n >> 1).Examplepackage main import "fmt" func PreviousPowOf2(n int) int{    n = n | (n >> 1)    n = n | (n >> 2)    n = n | (n >> 4)    n = ...

Read More

Golang Program to check the power of 4 of a given number

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 263 Views

ExamplesFor example, n = 12 => 12 is not the power of 4.For example, n = 64 => 64 is the power of 4.Approach to solve this problemStep 1 − Define a method that accepts a number n.Step 2 − Divide log(n) by log(4), store in res.Step 3 − If the floor of res is same as the res, then print that n is the power of 4.Step 4 − Else, print that n is not the power of 4.Examplepackage main import (    "fmt"    "math" ) func CheckPowerOf4(n int){    res := math.Log(float64(n)) / math.Log(float64(4))    if res ...

Read More

Golang Program to find the odd-occurring elements in a given array

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 361 Views

ExamplesFor example, arr = [1, 4, 5, 1, 4, 5, 1] => Odd-occurring element in the array is: 1Approach to solve this problemStep 1 − Define method that accepts an array.Step 2 − Declare a xor variable, i.e., xor := 0.Step 3 − Iterate input array and perform xor operation with each element of the array.Step 4 − At the end, return xor.Examplepackage main import (    "fmt" ) func FindOddOccurringElement(arr []int) int{    xor := 0    for i := 0; i < len(arr); i++ {       xor = xor ^ arr[i]    }    return xor ...

Read More

Golang Program to traverse a given binary tree in Preorder Traversal (Recursive)

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 744 Views

ExampleSuppose we have the following binary tree.Preorder Tree Traversal Output: 1, 2, 4, 5, 3, 6, 7.Approach to solve this problemStep 1 − If the root node of the given tree is nil, then return; else, follow the steps given below.Step 2 − Print the root node data.Step 3 − Traverse the Left sub-tree.Step 4 − Traverse the Right sub-tree.Examplepackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PreOrderTraversal(){    if root !=nil{       fmt.Printf("%d ", root.data)       root.left.PreOrderTraversal()       root.right.PreOrderTraversal()    } ...

Read More
Showing 41–50 of 852 articles
« Prev 1 3 4 5 6 7 86 Next »
Advertisements