Go Programming Articles

Page 4 of 86

Golang Program to Print the Largest Even and Largest Odd Number in a List

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

Enter the number of elements to be in the list: 5Element: 45Element: 20Element: 80Element: 93Element: 3Largest even number: 80Largest odd number 93Enter the number of elements to be in the list: 4Element: 23Element: 10Element: 34Element: 89Largest even number: 34Largest odd number 89StepsEnter the number of elements of to be in the list.Define a size variable.Initialize an array with size.Take user's input for array.Iterate in the array, and compare oddRes and oddEven numbers for the largest.Print the largest evenRes and oddRes.Examplepackage main import "fmt" func main() {    fmt.Printf("Enter the number of elements to be in the list:")    var size ...

Read More

Golang Program to Find the Numbers which are Divisible by 7 and Multiple of 5 in a Given Range

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

StepsTake in the upper and lower range and store them in separate variables.Use a for loop which ranges from the lower range to the upper range.Find the numbers which are divisible by both 5 and 7.Print those numbers.Case 1Enter the lower range: 1Enter the upper range: 1003570Case 2Enter the lower range: 400Enter the upper range: 700420455490525560595630665700ExplanationUser must enter the upper range limit and the lower range limit.The for loop ranges from the lower limit to the upper limit.The value of i ranges from the lower limit to the upper limit.Whenever the remainder of i divided by 5 and 7 is ...

Read More

Golang Program to Check if a Number is a Perfect Number

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

StepsRead an integer and store it in a variable.Initialize a variable to count the sum of the proper divisors to 0.Use a for loop and an if statement to add the proper divisors of the integer to the sum variable.Check if the sum of the proper divisors of the number is equal to the variable.Print the final result.Enter any number: 6The number is a Perfect number!Enter any number: 25The number is not a Perfect number!Examplepackage main import "fmt" func main(){    var n int    fmt.Print("Enter any number: ")    fmt.Scanf("%d", &n)    sum1 := 0    for i:=1; i

Read More

Golang Program to check if two numbers are Amicable Numbers

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

StepsRead two integers and store them in separate variables.Find the sum of the proper divisors of both the numbers.Check if the sum of the proper divisors is equal to the opposite numbers.If they are equal, they are amicable numbers.Print the final result.Enter number 1: 220Enter number 2: 284Amicable!Enter number 1: 349Enter number 2: 234Not Amicable!Examplepackage main import "fmt" func main(){    var a, b int    fmt.Print("Enter first number: ")    fmt.Scanf("%d", &a)    fmt.Print("Enter second number: ")    fmt.Scanf("%d", &b)    sum1 := 0    for i:=1; i

Read More

Golang Program to Find the Area of a Triangle Given All Three Sides

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

StepsRead all the three sides of the triangle and store them in three separate variables.Using the Heron's formula, compute the area of the triangle.Print the area of the triangle.Enter first side: 15Enter second side: 9Enter third side: 7Area of the triangle is: 20.69Enter first side: 5Enter second side: 6Enter third side: 7Area of the triangle is: 14.7ExplanationUser must enter all three numbers and store them in separate variables.First, the value of s is found which is equal to (a+b+c)/2Then, the Heron's formula is applied to determine the area of the triangle formed by all three sides.Finally, the area of the ...

Read More

Golang Program to Find the Gravitational Force Acting Between Two Objects

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 11-Mar-2026 496 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 268 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 400 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 702 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 734 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
Showing 31–40 of 852 articles
« Prev 1 2 3 4 5 6 86 Next »
Advertisements