Go Programming Articles

Page 4 of 86

Golang Program to Find the Gravitational Force Acting Between Two Objects

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

StepsRead both the masses and the distance between the masses and store them in separate variables.Initialize one of the variables to the value of gravitational constant, G.Then, the formula f=(G*m1*m2)/(r**2) is used to determine the force acting between the masses.Rounding off up to two decimal places, print the value of the force.Enter the first mass: 1000000Enter the second mass: 500000Enter the distance between the centres of the masses: 20Hence, the gravitational force is: 0.08 NEnter the first mass: 90000000Enter the second mass: 7000000Enter the distance between the centres of the masses: 20Hence, the gravitational force is: 105.1 NExplanationUser must enter ...

Read More

Golang Program to Print the Numbers in a Range (1, upper) without Using any Loops

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

StepsDefine a recursive function.Define a base case for that function that the number should be greater than zero.If the number is greater than 0, call the function again with the argument as the number minus 1.Print the number.Enter the upper limit: 512345Enter the upper limit: 1512..15Examplepackage main import (    "fmt" ) func printNo(number int){    if number >= 1{       printNo(number-1)       fmt.Println(number)    } } func main(){    var upper int    fmt.Print("Enter the upper limit: ")    fmt.Scanf("%d", &upper)    printNo(upper) }OutputEnter the upper limit: 5 1 2 3 4 5

Read More

Golang Program to Determine Recursively Whether a Given Number is Even or Odd

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

StepsTake a number from the user and store it in a variable.Pass the number as an argument to a recursive function.Define the base condition as the number to be lesser than 2.Otherwise, call the function recursively with the number minus 2.Then, return the result and check if the number is even or odd.Print the final result.Enter a number: 124Number is even!Enter a number: 567Number is odd!Examplepackage main import (    "fmt" ) func check(n int) bool{    if n < 2 {       return n % 2 == 0    }    return check(n - 2) } func ...

Read More

Golang program to define a binary tree

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

Example − In the given tree, the root node is 1, the root of its left sub tree is 2, and the root of its right sub tree is 3, ... so on.Preorder Tree Traversal Output: 1, 2, 4, 5, 3, 6, 7.Approach to solve this problemStep 1 − First, we’ll define the node structure.Step 2 − In the main method, we would create the above tree.Step 3 − Finally, we will perform the Preorder Tree Traversal.Examplepackage main import "fmt" type Node struct {    data int    left *Node    right *Node } func (root *Node)PreOrderTraversal(){    if root ...

Read More

Golang Program to check whether given positive number is power of 2 or not, without using any branching or loop

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

ExamplesConsider n = 16(00010000)Now find x = n-1 => 15(00001111) => x & n => 0Approach to solve this problemStep 1 − Define a method, where n and is an argument, returns type is int.Step 2 − Perform x = n & n-1.Step 3 − If x is 0, then the given number is power of 2; else not.Examplepackage main import (    "fmt"    "strconv" ) func CheckNumberPowerOfTwo(n int) int {    return n & (n-1) } func main(){    var n = 16    fmt.Printf("Binary of %d is: %s.", n, strconv.FormatInt(int64(n), 2))    flag := CheckNumberPowerOfTwo(n)    if ...

Read More

Golang Program to find the parity of a given number.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 994 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 202 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 305 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 273 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
Showing 31–40 of 852 articles
« Prev 1 2 3 4 5 6 86 Next »
Advertisements