Swap Two Numbers Without Using a Third Variable in Golang

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 %d and %d", a, b)    b = a + b    a = b - a    b = b - a    fmt.Printf("After swapping, numbers are %d and %d", a, b) } func main(){    swap(23, 45)    swap(56, 100) }OutputBefore swapping, numbers are 23 and 45 After swapping, numbers are 45 and 23 Before swapping, numbers are 56 and 100 After swapping, numbers are 100 and 56

Find Factorial of a Number Using Recursion in Go

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) int{    if num == 1 || num == 0{       return num    }    return num * factorial(num-1) } func main(){    fmt.Println(factorial(3))    fmt.Println(factorial(4))    fmt.Println(factorial(5)) }Output6 24 120

Search Element in Array Using Go

Kiran P
Updated on 04-Feb-2021 10:54:20

407 Views

Definition: A number is that is greater than 2 and divisible only by itself and 1.Examples: Prime numbers are 2, 3, 5, 7, 11, 13, 113, 119, ..., etc.Approach to solve this problemStep 1: Find square root of the given number, sq_root = √numStep 2: If the given number is divisible by a number that belongs to [2, sq_root], then print “Non Prime Number”Step 3: If not divisible by any number, then print “Prime Number”ProgramLive Demopackage main import (    "fmt"    "math" ) func checkPrimeNumber(num int) {    if num < 2 {       fmt.Println("Number must be greater than 2.")       return    }    sq_root := int(math.Sqrt(float64(num)))    for i:=2; i

Find Maximum Value in an Array Using Go Language

Kiran P
Updated on 04-Feb-2021 10:52:45

647 Views

ExamplesA1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Maximum number is 10A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Maximum number is 110Approach to solve this problemStep 1: Consider the number at the 0th index as the maximum number, max_num = A[0]Step 2: Compare max_num with every number in the given array, while iterating.Step 3: If a number is greater than max_num, then assign that number to max_num;Step 4: At the end of iteration, return max_num;ProgramLive Demopackage main import "fmt" func findMaxElement(arr []int) int {    max_num := arr[0]    for i:=0; i ... Read More

Find Minimum Value Element in Array using Go

Kiran P
Updated on 04-Feb-2021 10:52:24

889 Views

ExamplesA1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Minimum number is 0;A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Minimum number is 10;Approach to solve this problemStep 1: Consider the number at the 0th index as the minimum number, min_num = A[0].Step 2: Compare min_num with every number in the given array, while iterating.Step 3: If a number is smaller than min_num, then assign that number to min_num.Step 4: At the end of iteration, return min_num;ProgramLive Demopackage main import "fmt" func findMinElement(arr []int) int {    min_num := arr[0]    for i:=0; i

Check Whether a Given Number is Prime in Golang

Kiran P
Updated on 04-Feb-2021 10:52:02

5K+ Views

Definition: A number is that is greater than 2 and divisible only by itself and 1.Examples: Prime numbers are 2, 3, 5, 7, 11, 13, 113, 119, ..., etc.Approach to solve this problemStep 1: Find square root of the given number, sq_root = √numStep 2: If the given number is divisible by a number that belongs to [2, sq_root], then print “Non Prime Number”Step 3: If not divisible by any number, then print “Prime Number”ProgramLive Demopackage main import (    "fmt"    "math" ) func checkPrimeNumber(num int) {    if num < 2 {       fmt.Println("Number must be greater than 2.")       return    }    sq_root := int(math.Sqrt(float64(num)))    for i:=2; i

Adaptive vs Non-Adaptive Routing Algorithms

Moumita
Updated on 03-Feb-2021 08:01:46

6K+ Views

Routing Algorithms − A routing algorithm is a procedure that lays down the route or path to transfer data packets from source to the destination. There can be many paths from the source to the destination; routing algorithm mathematically computes the best path, i.e. “least – cost path”. Routing algorithms are broadly classified into adaptive and non-adaptive routing algorithms.Adaptive Routing Algorithms − Also known as dynamic routing algorithms, they makes routing decisions dynamically depending on the network conditions. It constructs the routing table depending upon the network traffic and topology.Non-Adaptive Routing Algorithms − Also known as static routing algorithms, they ... Read More

Sink Tree in Computer Networks

Moumita
Updated on 03-Feb-2021 07:59:15

7K+ Views

DefinitionSink trees of a given node in a computer network is a tree formed by connecting the node with all the other nodes of in the network with the optimal path, or more precisely the shortest path. Or, in other words, a sink tree is a minimal cost tree formed with the given node as the destination and all other nodes as sources. A popular measure for the path or the cost is the number of hops.ExplanationThe sink tree follows from the optimality principle. According to it, in a network of routers for data transmission, if a router ‘J’ lies ... Read More

Adaptive Routing Algorithms

Moumita
Updated on 03-Feb-2021 07:55:34

8K+ Views

Adaptive routing algorithms, also known as dynamic routing algorithms, makes routing decisions dynamically while transferring data packets from the source to the destination. These algorithms constructs routing tables depending on the network conditions like network traffic and topology. They try to compute computes the best path, i.e. “least – cost path”, depending upon the hop count, transit time and distance.Types of Adaptive Routing AlgorithmsThe three popular types of adaptive routing algorithms are shown in the following diagram −Centralized algorithm − In centralized routing, one centralized node has the total network information and takes the routing decisions. It finds the least-cost ... Read More

Non-Adaptive Routing Algorithms

Moumita
Updated on 03-Feb-2021 07:51:35

5K+ Views

Non-adaptive routing algorithms, also known as static routing algorithms, do not change the selected routing decisions for transferring data packets from the source to the destination. They construct a static routing table in advance to determine the path through which packets are to be sent.The static routing table is constructed based upon the routing information stored in the routers when the network is booted up. Once the static paths are available to all the routers, they transmit the data packets along these paths. The changing network topology and traffic conditions do not affect the routing decisions.Types of Non − adaptive ... Read More

Advertisements