Found 1082 Articles for Go Programming

Golang Program to Read Three Digits and Print all Possible Combinations from the Digits

Rishikesh Kumar Rishi
Updated on 02-Aug-2021 05:36:35

392 Views

Numbers are: a, b and c => 1, 2, 3Combination of (a, b, c) are: (1, 1, 1), (1, 2, 1), (1, 2, 2), (1, 2, 3), . . ., (3, 3, 3).StepsDefine the variables, a, b and c.Print statement for the first number and scan the number.Print statement for the second number and scan the number.Print statement for the third number and scan the number.Initialize an array with numbers, a, b and c.Iterate the array with iterator i, j and k.Print (i, j, k)th index number of the array.Example Live Demopackage main import "fmt" func main(){    var a, b, ... Read More

Golang Program to Read Two Numbers and Print their Quotient and Remainder

Rishikesh Kumar Rishi
Updated on 02-Aug-2021 06:06:15

277 Views

To print the Quotient and Remainder of two numbers, we can use the division operator and the modulo operator.Let's take an Example:a = 15 and b = 2Quotient is 15/2 = 7Remainder is 15 % 2 = 1StepsDefine the variables, a and b.Use print statement for the first number.Use print statement for the second number.Find the division of the numbers, a and b.Find the remainder of the numbers, a and b.Print the calculated division.Print the calculated remainder.Example Live Demopackage main import "fmt" func main(){    var a, b int    fmt.Print("Enter first number: ")    fmt.Scanf("%d", &a)    fmt.Print("Enter second number: ... Read More

Golang Program to Print all Numbers in a Range Divisible by a Given Number

Rishikesh Kumar Rishi
Updated on 02-Aug-2021 06:03:22

357 Views

Let's assume the lower and upper limit for the range is 2 and 10, respectively, and the given number is 2.Iterate in the range of 2 to 10 and find modulo of 2 and print them.StepsDefine variables for upper and lower limit for the range.Print statement for upper and lower limit.Take input from the user.Define a variable (n) to check the divisibility in a range.Take the input from users for n.Iterate in the range of lowerLimit and upperLimit.Find modulo with n in the range and print them.Example Live Demopackage main import "fmt" func main(){    var upperLimit, lowerLimit int    fmt.Printf("Enter ... Read More

Golang Program to read the marks of subjects and display the Grade

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 14:32:34

454 Views

Let's enter the marks: 89 56 90 67 99Sum of the marks is: 89+56+90+67+99 => 401Avg. = 401/5 = 80.1The steps are as follows:Define variables for 5 subjects.Enter marks for 5 subjects.Find average of the marks to find grade.Use if else block to print grade.Example Live Demopackage main import "fmt" func main(){    var sub1, sub2, sub3, sub4, sub5 int    fmt.Println("Enter marks of the five subjects:")    fmt.Scanf("%d", &sub1)    fmt.Scanf("%d", &sub2)    fmt.Scanf("%d", &sub3)    fmt.Scanf("%d", &sub4)    fmt.Scanf("%d", &sub5)    avg:=(sub1+sub2+sub3+sub4+sub5)/5    if avg>=90{       print("Grade: A")    }else if avg>=80 && avg=70 && avg=60 && avg

Golang Program to Read a Number (n) and Compute (n+nn+nnn)

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 14:17:48

250 Views

Let's read a number, n=5Then, nn=55 and then nnn=555res = 5 + 55 + 555 => 615To read a number (n) and compute (n+nn+nnn), we can take the followingStepsDefine a variable, n.Print a statement to get the number, n.Take user input for variable, n.Make an expression for (n + nn + nnn).Convert the expression into numbers.Compute the sum of the expression.Example Live Demopackage main import (    "fmt"    "strconv" ) func main(){    var n int    fmt.Print("Enter value of n: ")    fmt.Scanf("%d", &n)    t1 := fmt.Sprintf("%d", n)    t2 := fmt.Sprintf("+%d%d", n, n)    t3 := fmt.Sprintf("+%d%d%d", ... Read More

Golang Program to Calculate the Average of Numbers in a Given List

Rishikesh Kumar Rishi
Updated on 30-Jul-2021 15:41:19

995 Views

Input array is: [2, 4, 1, 6, 5]Sum = 2 + 4 + 1 + 6 + 5 => 18Average = 18/5 => 3.6 ~ 3To calculate the average of numbers in a given list, we can take following steps −Let's take an input list of numbers.Find the sum of numbers using sum() method.The sum method calculates the sum of numbers by iterating the given list.Print the average by dividing the sum with the length of the given list.Example Live Demopackage main import (    "fmt" ) func sum(arr []int) int{    result := 0    for _, i :=range arr ... Read More

Golang Program to create a Class that can perform basic Calculator Operations

Rishikesh Kumar Rishi
Updated on 30-Jul-2021 15:38:15

893 Views

To create a class that can perform basic calculator operations, we can take following StepsWe can define a Calculator class with two numbers, a and b.Define a member method to calculate the addition of two numbers.Define a member method to calculate the multiplication of two numbers.Define a member method to calculate the division of two numbers.Define a member method to calculate the subtraction of two numbers.In the main method, declare two variables, a and b.Get an instance of Calculator.Initialize a choice variable, based on which mathematical operations could be performed.Example Live Demopackage main import (    "fmt" ) type Calculator struct ... Read More

Golang Program to Create a Class and Compute the Area and Perimeter of a Circle

Rishikesh Kumar Rishi
Updated on 30-Jul-2021 15:25:58

353 Views

To compute the area and perimeter of a circle, we can take following steps −Define a struct with circle properties such as radius.Define a method to calculate the area of the circle.Define a method to calculate the perimeter of the circle.In the main method, take the user's input for circle's radius.Instantiate the circle with the radius.Print the area of the circle.Print the perimeter of the circle.Example Live Demopackage main import (    "fmt"    "math" ) type Circle struct {    radius float64 } func (r *Circle)Area() float64{    return math.Pi * r.radius * r.radius } func (r *Circle)Perimeter() float64{   ... Read More

Golang Program to Find the Area of a Rectangle Using Classes

Rishikesh Kumar Rishi
Updated on 30-Jul-2021 15:17:39

399 Views

To find the area of a rectangle using classes, we can take following StepsDefine a struct with rectangle properties such as breadth and length.Define a method to calculate the area of the rectangle.In the main method, instantiate an object of rectangle.Call the struct method, i.e., Area, to calculate the area of the rectangle.Print the area of the rectangle.Example Live Demopackage main import (    "fmt" ) type Rectangle struct {    breadth int    len int } func (r *Rectangle)Area() int{    return r. len * r.breadth } func main(){    rectangle := Rectangle{       breadth: 10,     ... Read More

Golang Program to Read the Contents of a File

Rishikesh Kumar Rishi
Updated on 30-Jul-2021 09:44:00

737 Views

pre.prettyprint{width:99%!important;} a.demo{top:12px!important; float:right!important;}To read the contents of a file, we can take following steps −We can create a file called test.txt.Clean-up using defer statement.Write the contents of string.Call ReadFile() method to read the file.ReadFile reads the file named by filename and returns the contents.Print the content of the file.Example Live Demopackage main import (    "fmt"    "io/ioutil"    "log"    "os" ) func CreateFile() {    file, err := os.Create("test.txt")    if err != nil {       log.Fatalf("failed creating file: %s", err)    }    defer file.Close()    _, err = file.WriteString("Welcome to Tutorials Point")    if err ... Read More

Advertisements