Go Programming Articles

Page 2 of 86

Golang Program to Print Odd Numbers Within a Given Range

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

To print odd number in a range, we can take two inputs, a and b, for lower and upper limits.Examplea = 2 and b = 9Numbers between a and b are: 2, 3, 4, 5, 6, 7, 8, 9Odd numbers are: 3, 5, 7, 9StepsDefine two numbers, a and b.Take user inputs for the numbers, a and b.Iterate the number between a and b.Find the modulo of 2, print the number if modulo of 2 of that number is not zero.Examplepackage main import "fmt" func main(){    var a, b int    fmt.Print("Enter lower limit number: ")    fmt.Scanf("%d", &a) ...

Read More

Golang Program to Find the Smallest Divisor of an Integer

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

Consider that the integer is: 75Divisor of that integer is: 3, 5, 15, ..., 75The smallest divisor is: 3StepsTake an integer from the user.Initialize a variable (res) with that number.Use a for loop where the value of i ranges from 2 to the integer.If the number is divisible by i, compare with res. If res > i, then update res with i.Exit from the loop and print res.Examplepackage main import "fmt" func main(){    var n int    fmt.Print("Enter the number: ")    fmt.Scanf("%d", &n)    res := n    for i:=2; i

Read More

Golang Program to Count the Number of Digits in a Number

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

Suppose the number is: 123456Count of digits in the given number is: 6To count the number of digits in a number, we can take followingStepsTake the value of the integer and store in a variable.Using a while loop, get each digit of the number and increment the count each time a digit is obtained.Print the number of digits in the given integer.Examplepackage main import "fmt" func main(){    var n int    fmt.Print("Enter the number: ")    fmt.Scanf("%d", &n)    count := 0    for n >0 {       n = n/10       count++    } ...

Read More

Golang Program to print all integers between a range that aren't divisible by either 2 or 3

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

Let's assume the range is from 0 to 50. We have to print all the integers that aren't divisible by either 2 or 3.StepsUse a for loop ranging from 0 to 50.Then, use an if statement to check if the number isn't divisible by both 2 and 3.Print the numbers satisfying the condition.ExplanationThe for loop ranges from 0 to 50.The number is divided by 2 and 3.If the remainder ≠ 0, the number isn't divisible by either 2 and 3.The number satisfying the condition is printed.Examplepackage main import "fmt" func main(){    for i:=1; i

Read More

Goland Program to Read a Number (n) And Print the Series "1+2+…..+n= "

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

StepsTake a value from the user and store it in a variable (n).Use a for loop where the value of i ranges between the values of 1 and n.Print the value of i and '+' operator.Find the sum of elements in the list.Print '=' followed by the total sum.Exit.ExplanationUser must first enter the value and store it in a variable, n.The for loop enables i to range between 1 and n (as n+1 is not included).For each iteration, the value of i is printed.'+' operator is printed only if iExamplepackage main import "fmt" func main(){    var n int   ...

Read More

Golang Program to Read a Number (n) and Print the Natural Numbers Summation Pattern

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

Let's suppose the number is: 4Then, the Summation Patten would be:1 = 11 + 2 = 31 + 2 + 3 = 61 + 2 + 3 + 4 = 10StepsTake a value from the user and store it in a variable, n.Use two for loops where the value of t ranges between the values of 1 and n and the value of i ranges between 1 and t.Print the value of i and '+' operator.Find the sum of elements in the list.Print '=' followed by the total sum.Exit.Examplepackage main import "fmt" func main(){    var n int    fmt.Print("Enter number: ")    fmt.Scanf("%d", &n)    for t:=1; t

Read More

Golang Program to Print an Identity Matrix

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

The steps to print an identity matrix using Golang is as follows :Take a value from the user and store it in a variable, n.Use two for loops where the value of j ranges between the values of 0 and n-1 and the value of i also ranges between 0 and n-1.Print the value of 1 when i is equal to j, and 0 otherwise.Case 1:Enter a number: 4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1Case 2:Enter a number: 5 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1Examplepackage main import "fmt" func main(){    var n int    fmt.Print("Enter a number: ")    fmt.Scanf("%d", &n)    for i:=0; i

Read More

Golang Program to Print an Inverted Star Pattern

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

StepsTake a value from the user and store it in a variable, n.Use a for loop where the value of i ranges between the values of n-1 and 0 with a decrement of 1 with each iteration.Multiply the empty spaces with n-i and '*' with i and print both of them.Exit.ExplanationUser must first enter the value and store it in a variable, n.The for loop enables i to range between n-1 and 0 with a decrement of 1 with each iteration.For each iteration, " " is multiplied with n-i and '*' is multiplied with i to ensure correct spacing of ...

Read More

Golang Program to Compute Simple Interest Given all the Required Values

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

StepsRead the values for principal amount, rate and time.Using the formula, compute the simple interest.Print the value for the computed interest.Enter the principal amount: 200Enter the time(years): 5Enter the rate: 5The simple interest is: 50Enter the principal amount: 70000Enter the time(years): 1Enter the rate: 4The simple interest is: 2800ExplanationUser must enter the values for the principal amount, rate and time.The formula: (amount*time*rate)/100 is used to compute the simple interest.The simple interest is later printed.Examplepackage main import "fmt" func main() {    var principal, time, rate int    fmt.Print("Enter the principal amount: ")    fmt.Scanf("%d", &principal)    fmt.Print("Enter the time in ...

Read More

Golang Program to Check Whether a Given Year is a Leap Year

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

StepsTake the value of the year as input.Using an if-statement, check whether the year is a leap year or notPrint the final result.Enter the year to be checked: 2016The year is a leap year!Enter the year to be checked: 2005The year isn't a leap year!ExplanationUser must first enter the year to be checked.The if statement checks if the year is a multiple of 4 but isn't a multiple of 100 or if it is a multiple of 400 (not every year that is a multiple of 4 is a leap year).Then, the result is printed.Examplepackage main import "fmt" func main() ...

Read More
Showing 11–20 of 852 articles
« Prev 1 2 3 4 5 86 Next »
Advertisements