Found 31994 Articles for Programming

Haskell Program to convert the Binary number to Gray code using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:58:18
In Haskel we can use recursion along with helper function toconvert the Binary number to Gray code. In the first example, we are going to use base case, (grayCode "" = "" and grayCode [x] = [x]) and recursive case, grayCode (x:y:xs) = x : grayCode (xs ++ [if x == y then '0' else if x == '0' then '1' else '0'])). Where as in the second example, we are going to use two helper functions along with recursion. Method 1: Converting the Binary number to Gray Code using recursion In this method, the grayCode function is defined to ... Read More

Haskell Program to convert the decimal number to binary using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:57:26
In Haskell, we will convert the decimal number to binary by using recursion and tail-recursion. In the first example, we are going to use base case, (decToBin 0 = "0" and decToBin 1 = "1") and recursive case, (decToBin n = decToBin (n `div` 2) ++ show (n `mod` 2)). Whereas in second example, we are going to use tail-recursive function. Algorithm Step 1 − The user defined, decToBin function is defined with base and recursive case as, Example 1 and 2 − decToBin 0 = "0" decToBin 1 = "1" decToBin n = decToBin ... Read More

Haskell Program to find the GCD of two given numbers using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:52:46
In Haskell, we can find the the GCD of two given numbers by using recursion along with gcd function and tail-recursion. In the first and second examples, we are going to use base case, (gcd a 0 = a) and recursive case, gcd a b = gcd b (a `mod` b)) and in the third example, we are going to use tail-recursive function. In the following examples we define a gcd function that takes two Int arguments a and b. The function uses pattern matching to handle two cases − If b is 0, the function returns a ... Read More

Haskell Program to find the LCM of two given numbers using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:51:19
In Haskell, we will find the the LCM of two given numbers by using recursion along with gcd and max function. In the first example, we are going to use (gcd) and (lcmOfTwoNumbers) function and in the second example, we are going to use (lcm) and (lcmOfTwoNumbers a b = lcm a b (max a b)) function. Algorithm Step 1 − The Prelude library is imported for hiding the gcd function. Step 2 − Define two user-defined functions - gcd and lcmOfTwoNumbers , Step 3 − Program execution will be started from main function. The main() function has whole ... Read More

Haskell Program to find the given number is PRIME or not using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:50:19
In Haskell, we can find the given number is PRIME or not by using recursion along with helper function. In the first example, we are going to use (isPrime n | n Bool isPrime n | n Integer -> Bool isPrimeHelper n d | d > (n `div` 2) = True | n `mod` d == 0 = False | otherwise = isPrimeHelper n (d + 1) main :: IO () main = do let ... Read More

Haskell Program to find the reverse of a given number using recursion

Akhil Sharma
Updated on 27-Mar-2023 10:48:52
In Haskell, we can find reverse of a given number by using recursion along with helper function. In the first example, we are going to use (reverseNumHelper n rev) function and in the second example, we are going to use (reverseNum n | n < 10 = n| otherwise = (n `mod` 10) * (10 ^ numDigits (n `div` 10)) + reverseNum (n `div` 10)) function. Finding the reverse of a given number using recursion using the helper function In this method, the reverseNumHelper function takes two arguments: n and rev. n is the number to reverse and rev is ... Read More

Golang program to get the hash collection values as an array

Akhil Sharma
Updated on 27-Mar-2023 10:19:03
In Go programming language, a hash collection contains a hashmap which stores the values as key:value pairs for the efficient execution of the programs. In this article we will use two examples to get the hash collection values as an array. In the first example, we will create a slice and append the strings of map in that slice, in the second example we will hash the string and store it inside the map. In this way we will print the array. Syntax func make ([] type, size, capacity) The make function in go language is used to create ... Read More

Golang program to check a given key exists in the hash collection or not

Akhil Sharma
Updated on 27-Mar-2023 10:11:48
In Golang we have inbuild function like ok idiom to check whether a given key exist in hash collection or not. Hashmap is a collection of values paired with key in hashmap collection. In this article, we will create a hashmap using the built-in function then we will use ok idiom that returns true false value to check whether a key exists in the map or not. In this way a success or failure statement will be printed. Algorithm Create a package main and declare fmt(format package) in the program where main produces executable codes and fmt helps in ... Read More

Golang program to merge two hash collections

Akhil Sharma
Updated on 27-Mar-2023 10:11:11
In golang we can merge two hash collection by using map function. A hashmap is present in hash collections. It stores key:value pairs. We can perform various operations on a hashmap like merging them. In this article we will merge two hashmaps using a method. In that method, we will create an additional map to store the merged key:value pairs. Then, the map will be printed on the terminal using fmt package. Algorithm Create a package main and declare fmt(format package) in the program where main produces executable codes and fmt helps in formatting input and output. ... Read More

Golang program to convert the hash collection into the array

Akhil Sharma
Updated on 27-Mar-2023 10:09:59
In Go programming language, a hash collection contains a hashmap which holds values in form of key:value pairs. Here, in this particular program we will convert the map into array which is of fixed size and can be accessed via indexing. We will use two examples to execute the program. In the first example, we will use an index variable to add the values in the array and in the second example we will use an append method to add the values in the array. Syntax func make ([] type, size, capacity) The make function in go language is ... Read More
Previous 1 ... 3 4 5 6 7 ... 3200 Next
Advertisements