Kiran P has Published 107 Articles

Write a Golang program to find the frequency of an element in an array

Kiran P

Kiran P

Updated on 04-Feb-2021 11:13:59

335 Views

ExamplesIn the Input array, arr = [2, 4, 6, 7, 8, 1, 2]Frequency of 2 in given array is 2Frequency of 7 is 1Frequency of 3 is 0.Approach to solve this problemStep 1: Define a function that accepts an array and a numStep 2: Declare a variable count = 0.Step ... Read More

Write a Golang program to calculate the sum of elements in a given array

Kiran P

Kiran P

Updated on 04-Feb-2021 11:10:48

4K+ Views

ExamplesInput arr = [1, 3, 4, 5, 6] => 1+3+4+5+6 = 19Input arr = [5, 7, 8, 9, 1, 0, 6] => 5+7+8+9+1+0+6 = 36Approach to solve this problemStep 1: Define a function that accepts an array.Step 2: Declare a variable, res = 0.Step 3: Iterate the array and add elements to res.Step 4: Return res.ProgramLive Demopackage main import "fmt" func findArraySum(arr []int) int{    res := 0    for i:=0; i

Write a Golang program to convert a binary number to its decimal form

Kiran P

Kiran P

Updated on 04-Feb-2021 11:10:33

2K+ Views

ExamplesInput binary_num = 1010111 => decimal number = 64+0+16+4+2+1 = 87Input binary_num = 10101 => decimal number = 21Approach to solve this problemStep 1: Define a function that accepts a binary number, binary_num, declare decimalNum = 0, index = 0Step 2: Start for loop until binary_num becomes 0.Step 3: Find ... Read More

Write a Golang program to reverse a number

Kiran P

Kiran P

Updated on 04-Feb-2021 11:03:30

3K+ Views

ExamplesInput num = 1432 => output = 2341Input num = 9878 => output = 8789Input num = 6785 => output = 5876Approach to solve this problemStep 1: Define a function that accepts a number (num); type is int.Step 2: Define res = 0 variable and start a loop until num ... Read More

Write a Golang program to reverse an array

Kiran P

Kiran P

Updated on 04-Feb-2021 11:03:15

3K+ Views

ExamplesInput arr = [3, 5, 7, 9, 10, 4] => [4, 10, 9, 7, 5, 3]Input arr = [1, 2, 3, 4, 5] => [5, 4, 3, 2, 1]Approach to solve this problemStep 1: Define a function that accepts an array.Step 2: Start iterating the input array.Step 3: Swap first ... Read More

Write a Golang program to reverse a string

Kiran P

Kiran P

Updated on 04-Feb-2021 11:02:57

5K+ Views

ExamplesInput str = “himalaya” => Reverse String would be like => “ayalamih”Input str = “mountain” => Reverse String would be like => “niatnuom”Approach to solve this problemStep 1: Define a function that accepts a string, i.e., str.Step 2: Convert str to byte string.Step 3: Start iterating the byte string.Step 4: ... Read More

Write a Golang program to find the sum of digits for a given number

Kiran P

Kiran P

Updated on 04-Feb-2021 11:02:02

2K+ Views

Examplesnum = 125 => 1 + 2 + 5 = 8num = 101 => 1 + 0 + 1 = 2num = 151 => 1 + 5 + 1 = 7Approach to solve this problemStep 1: Define a function that accepts numbers(num); type is int.Step 2: Start a true loop ... Read More

Write a Golang program to print the Fibonacci series

Kiran P

Kiran P

Updated on 04-Feb-2021 11:01:25

6K+ Views

Definition: In a Fibonacci series, the next number would be the summation of its two previous numbers, series starting from 0 and 1.ExamplesPrint a fibonacci series up to num = 10;Series: 1, 2, 3, 5, 8, next is 13 but greater than 10;Approach to solve this problemStep 1: Define a function ... Read More

Write a Golang program to swap two numbers without using a third variable

Kiran P

Kiran P

Updated on 04-Feb-2021 10:55:07

1K+ Views

Approach to solve this problemStep 1: Define a function that accepts two numbers, type is int.Step 2: Find b = a + b;Step 3: Then a = b – a and b = b – aProgramLive Demopackage main import "fmt" func swap(a, b int){    fmt.Printf("Before swapping, numbers are ... Read More

Write a Golang program to find the factorial of a given number (Using Recursion)

Kiran P

Kiran P

Updated on 04-Feb-2021 10:54:40

3K+ Views

ExamplesFactorial of 5 = 5*4*3*2*1 = 120Factorial of 10 = 10*9*8*7*6*5*4*3*2*1 =Approach to solve this problemStep 1: Define a function that accepts a number (greater than 0), type is int.Step 2: If the number is 1, then return 1.Step 3: Otherwise, return num*function(num-1).ProgramLive Demopackage main import "fmt" func factorial(num int) ... Read More

Advertisements