Found 34472 Articles for Programming

How to Pass an Array to the Function in Golang?

Aman Sharma
Updated on 10-Jul-2023 17:04:40

745 Views

In programming, to make the code more modular, usable, and readable we break the code into different functions. For example, we have to sort some set of numbers for which we will create a separate function. For this, we have a data structure called arrays in programming that is nothing but a collection of multiple values of the same type. Once the array of numbers is sorted then at the end of that function we have to return the sorted elements so in this article we will see how to return the array. Syntax The syntax to declare a function ... Read More

How to pass a string to the function in Golang?

Aman Sharma
Updated on 10-Jul-2023 16:59:44

1K+ Views

In this tutorial, we are going to learn about how to pass a string in function as an argument in Golang. In programming, to make the code more modular, usable, and readable we break the code into different small blocks and these blocks are known as functions. If you have created a string of alphabets and you want to perform some operations on the string then you need to pass that string to the function by passing it as an argument. This tutorial will explain this concept with the help of two examples. In the first example, we will ... Read More

How to Find Whether the given Number is PRIME or not using Recursion in Golang?

Aman Sharma
Updated on 10-Jul-2023 16:48:16

163 Views

In mathematics, there are numbers that can be divisible by 1 or by itself, and such numbers are called Prime numbers. For example, 2, 3, 5, 7 … etc. In programming, we can create a program to check number is prime or not. In this article, we will use the concept of recursion which we call the function within the function to create a program to check number is prime or not. Example 1 In this example, we are going to create a recursive function with two parameters one is the number and another one is the divisor. The ... Read More

How to find the Tangent of a given Radian Value in Golang?

Aman Sharma
Updated on 10-Jul-2023 16:40:12

51 Views

The ratio of the radian’s adjacent side and the opposite side is known as the tangent of the given radian. Golang language has many packages with predefined functions that the developer can use without writing the complete logic. To perform the mathematical operations and logic we have a math package in Golang. We will use this package only to find the Tangent of a given radian value. We will also see how to import the package and also how to call a function this package consists of by writing a Golang code. Tangent Definition Tangent is a function that ... Read More

How to Check Whether a Number is Even or Odd in Golang?

Aman Sharma
Updated on 10-Jul-2023 16:08:51

2K+ Views

In this tutorial, we are going to learn how we can check whether the number is even or odd. The number that can be divisible by 2 is an even number and the number that is not divisible by two is an odd number. This tutorial includes three different ways to achieve this Moulous Operator − In the first method, we are using the modulus (%) operator. This operator is used to find the remainder of two numbers. In our case, we will do the modulus of the number with 2 and if it returns 0 then ... Read More

How Does One Install Pip in a Docker Container using a Dockerfile?

Aman Sharma
Updated on 10-Jul-2023 15:59:34

6K+ Views

In this tutorial, we are going to learn how we can install pip in a Docker container using a Dockerfile. This tutorial will cover the creation of the Dockerfile and then we will see the docker command to build a docker image from the Dockerfile, and in last we will see the docker command to run a container based on that image and see whether PIP is installed properly or not. Prerequisites There are some prerequisites to creating and building the Dockerfile as mentioned below. The stable version of Docker should be installed. Create a file in any folder ... Read More

Golang Program to Sort an Array of 0’s, 1’s and 2’s

Aman Sharma
Updated on 10-Jul-2023 15:49:33

68 Views

In Golang, like other programming languages, we can code the logic to sort an array that has 0’s, 1’s, and 2’s as elements. Sorting means assigning the data either in increasing order or in decreasing order. This is one of the famous questions on the array asked in interviews. There can be two approaches to achieve this that we are going to explore one by one. For example, we have an array 2, 1, 0, 0, 1, 2, and after sorting the array will look like 0, 0, 1, 1, 2, 2. Method 1 In this example, we are going ... Read More

Golang Program to Implement Kaden's Algorithm

Aman Sharma
Updated on 10-Jul-2023 17:34:03

95 Views

There is a famous maximum sum subarray problem, in which we have a 1 Dimensional array and must find the maximum sum in that subarray. To solve this problem, the most naive approach will be to find all the sub − arrays, sum their elements, and return the maximum, but the time complexity will be O(N*N). To reduce this there is an algorithm that will reduce the time complexity from O(N*N) to O(N) named as Kadens Algorithm. In programming, there are algorithms based on the Dynamic programming concept in which the problem is broken into sub − problems and the ... Read More

Golang Program to Implement Linear Search Algorithm

Aman Sharma
Updated on 10-Jul-2023 15:44:46

256 Views

In programming, to search for anything from an array, linked List, or from any other data structures we have a few search algorithms, one of which is Linear search. In the linear search, we iterate over the data structure from starting and search for the element till the last index. The advantage of a linear search algorithm is that we can perform this search of both sorted and unsorted data. The disadvantage is that for sorted or unsorted both kinds of data it will take the same amount of time to find an element. For example, we have an array ... Read More

Golang Program to Implement Binary Search Algorithm

Aman Sharma
Updated on 10-Jul-2023 17:26:38

298 Views

In programming, to search for anything from an array, linked List, or any other data structures we have a few search algorithms, one of which is binary search. In binary search, the prerequisite is that the data should be sorted. In binary search, we follow the divide and conquer approach in which we divide the data by applying some conditions and then perform the operation on that data only. In this way, we reduce the time complexity. For example, if we have an array of elements {20, 44, 45, 54, 67, 88, 91} and we want to find 44 then ... Read More

Advertisements