Found 33676 Articles for Programming

Swift Program to calculate the sum of left diagonal of the matrix

Ankita Saini
Updated on 19-Jul-2023 14:26:19

252 Views

A matrix is an arrangement of numbers in rows and columns. Matrix can be of various type like square matrix, horizontal matrix, vertical matrix etc. So here we calculate the sum of the left diagonal of the square matrix using Swift Programming. A square matrix is a matrix in which the number of rows and columns are same. For example 2x2, 5x5, etc. For example, we have the following matrix − Matrix = 3 4 5 1 5 3 2 2 1 ... Read More

Golang program to include a module inside the class

Akhil Sharma
Updated on 01-Mar-2023 12:00:19

156 Views

In Go programming language, a new module is initiated using init command after which we can import other modules in our code. There is no concept of class in Go, only structs are used. In this article, we will use one example to include a module inside the class. In this example, we will create a Circle struct whose area will be printed on the console. For this example, we will import math and fmt packages outside the struct but they can be used anywhere. The print statement is executed using Printf function from the fmt package. Algorithm Import ... Read More

Golang program to create a block using curly braces

Akhil Sharma
Updated on 01-Mar-2023 11:59:43

702 Views

We can create a block in golang using curly braces and then create a variable inside the block to have the scope only inside the block and not outside it. In this article, we will use three examples to create a block using curly braces. In the first example few statements will be printed inside and outside the block, in the second example comparison value will be printed inside the block and in the third example function will be used to demonstrate the use of block. Algorithm Import the required packages in the program Create a main function In ... Read More

Golang program to convert the hash collection into string

Akhil Sharma
Updated on 01-Mar-2023 11:58:41

896 Views

Golang has json package that is used for converting the hask collection into strin. A hashmap belongs to hash collection which stores the data as key:value pairs, whereas a string is a sequence of characters and it is immutable. In this article, we will use two examples to convert the hash collection into string. In the first example, the Json package will be used to convert the map to encoded byte array whereas in the second example sort package will be use along the loops. Syntax json.Marshal() This function belongs to the JSON package and it converts the value ... Read More

Golang program to create a module with constant

Akhil Sharma
Updated on 19-Jul-2023 14:27:09

118 Views

In the Go programming language, a module is a collection of packages and it helps to manage them and their dependencies. In this article, we will create a module with constant using two examples. In the first example, we will create a constant string that will be returned by the function and that function will be called from the main of another module. In the second example, a function will be created to return the string and print when called by the main of another module. Algorithm Import the required packages in the program Create a constant string in ... Read More

Golang program to demonstrate the time arithmetic

Akhil Sharma
Updated on 01-Mar-2023 11:56:22

648 Views

We are going to calculate the current time using the Now function from time package in Go programming language for demonstrating the time arithmetic. The time arithmetic is calculated using mathematical functions. In both examples, we will use mathematical calculations to find the past, future and the duration using Sub function. The output is printed using Println function from the fmt package. Syntax func Now() Time The Now() function is defined in time package. This function generates the current local time. To use this function, we have to first import the time package in our program. func sub() ... Read More

Haskell Program to create a simple recursive function

Akhil Sharma
Updated on 01-Mar-2023 11:06:59

2K+ Views

In this article, we are going to understand how to create a simple function in Haskell using user-defined function. Here, the user-defined recursive functions are defined that will contain base case and recursive case. Then, these functions are being called recursively by passing the argument to it. In all the examples, we are going to define certain functions to perform certain tasks. All these functions include, base and recursive cases. The function defined includes factorial, fibonacci, sumList, etc. Algorithm Step 1 − The user defined recursive function is defined with base and recursive case. Step 2 − ... Read More

Haskell Program to return multiple values from the function

Akhil Sharma
Updated on 01-Mar-2023 11:06:28

1K+ Views

In Haskell, we can return multiple values from the function by using user-defined function along with tuple, custom data type and Maybe data type. In the first example, we are going to use (myFunction = ("Hello, World!", 42)) and in the second example, we are going to use (data MyData = MyData { stringValue :: String, intValue :: Int }). And in third example, we are going to use, (myFunction :: Maybe (String, Int)). Method 1: Using Tuple In this method, a function can return multiple values by using a tuple. A tuple is a data structure that holds multiple ... Read More

Haskell Program to return a string from the function

Akhil Sharma
Updated on 01-Mar-2023 11:05:22

1K+ Views

In this article, we are going to learn how to return a string from a function using user-defined function along with record syntax and let binding. In the first example, we are going to use (myFunction = "Hello, World!") function and in the second example, we are going to use (myFunction = stringValue myData). And in third example, we are going to use let binding, (myFunction = let str = "Using let binding!" in str). Method 1: Returning a string from the user-defined function In this method, the user-defined functions are defined that will contain the function definition and return ... Read More

Haskell Program to pass a string to the function

Akhil Sharma
Updated on 01-Mar-2023 11:04:31

519 Views

This article will help us learn how to pass a string to the function in Haskell with identity function and lambda expression. In the first example, we are going to use (myFunction inputString = inputString) function and in the second example, we are going to use (myFunction = id). And in third example, we are going to use lambda expression, (myFunction = \inputString -> inputString). Method 1: Passing a string to the user-defined function In this method, the user-defined functions are defined that will contain the function definition with some returning value and is being called by passing a string ... Read More

Advertisements