Print Largest Even and Largest Odd Number in a List using Golang

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:47:15

545 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.Example Live Demopackage main import "fmt" func main() {    fmt.Printf("Enter the number of elements to be in the list:")    var ... Read More

Print Sum of Positive and Negative Numbers in a List using Go

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:45:21

353 Views

StepsRead a number of elements to be in a list.Take the elements from the user using a for loop and append to a list.Using a for loop, get the elements one by one from the list and check if it is positive or negative.If it is positive, check if it is odd or even and find the individual sum.Find the individual sum of negative numbers.Print all the sums.Enter the number of elements to be in the list: 4Element: -12Element: 34Element: 35Element: 89Sum of all positive even numbers: 34Sum of all positive odd numbers: 124Sum of all negative numbers: -12Enter the ... Read More

Print Multiplication Table of a Given Number in Golang

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:35:29

1K+ Views

StepsRead a number and store it in a variable.Print the multiplication table of the given number.Enter the number to print the table for: 77 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 357 x 6 = 427 x 7 = 497 x 8 = 567 x 9 = 637 x 10 = 70Enter the number to print the table for: 1717 x 1 = 717 x 2 = 3417 x 3 = 5117 x 4 = 6817 x 5 = 8517 x 6 = 10217 x 7 = 11917 x 8 = 13617 x 9 = 15317 x 10 = 170ExplanationUser must enter a number.Using a print statement, print the multiplication table of the given number.Example Live Demopackage main import "fmt" func main(){    var n int    fmt.Print("Enter the number to print the multiplication table:")    fmt.Scanf("%d", &n)    for i:=1; i

Generate All Divisors of an Integer in Go

prashanth nallla
Updated on 31-Jul-2021 15:34:20

660 Views

StepsTake the value of the integer and store it in a variable.Use a for loop and an if statement to generate the divisors of the integer.Print the divisors of the number.Enter an integer:25The divisors of the number are:1525The divisors of the number are: 2012451020ExplanationUser must first enter the value and store it in a variable.Use a for loop to generate numbers from 1 to n.Using an if statement, check if the number divided by i gives the remainder as 0 which is basically the divisor of the integer.Print the divisors of the number.Example Live Demopackage main import "fmt" func main(){   ... Read More

Convert Celsius to Fahrenheit in Go

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:33:13

510 Views

StepsTake the value of temperature in Celsius and store it in a variable.Convert it to Fahrenheit.Print the final result.Enter the temperature in Celsius: 32Temperature in Fahrenheit is: 89.6Enter the temperature in Celsius: 48Temperature in Fahrenheit is: 118.4ExplanationUser must first enter the value of temperature in Celsius.Using the formula: f=(c*1.8)+32, convert Celsius to Fahrenheit.Print the temperature in Fahrenheit.Example Live Demopackage main import "fmt" func main(){    var n int    fmt.Print("Enter the temperature in Celsius:")    fmt.Scanf("%d", &n)    f:=(float32(n)*1.8)+32    fmt.Println("Temperature in Fahrenheit is:", f) }OutputEnter the temperature in Celsius:32 Temperature in Fahrenheit is: 89.6Read More

Convert Centimeters to Feet and Inches in Go

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:32:26

302 Views

StepsTake the height in centimeters and store it in a variable.Convert the height in centimeters into inches and feet.Print the length in inches and feet.Enter the height in centimeters: 50The length in inches: 19.7The length in feet: 1.64Enter the height in centimeters: 153The length in inches: 60.28The length in feet: 5.02ExplanationUser must enter the height in centimeters.The height in centimeters is multiplied by 0.394 and stored in another variable which now contains the height in inches.The height in centimeters is multiplied by 0.0328 and stored in another variable which now contains the height in feet.The converted height in inches and ... Read More

Check Whether a Given Year is a Leap Year in Go

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:31:40

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.Example Live Demopackage main import "fmt" func ... Read More

Golang Program to Print an Inverted Star Pattern

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:30:43

329 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

Compute Simple Interest in Go

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:11:05

165 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.Example Live Demopackage 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 ... Read More

Golang Program to Print an Identity Matrix

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:09:29

248 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 1Example Live Demopackage main import "fmt" func main(){    var n int    fmt.Print("Enter a number: ")    fmt.Scanf("%d", &n)    for i:=0; i

Advertisements