Akhil Sharma

Akhil Sharma

507 Articles Published

Articles by Akhil Sharma

Page 13 of 51

Golang Program to open a file in append mode

Akhil Sharma
Akhil Sharma
Updated on 03-May-2023 1K+ Views

In Go programming language, os and io packages can be used for performing various operations on external files like copying, editing, and even appending them. In this article, we will learn two Examples to open a file in append mode. In the first Example we will use os.OpenFile function from os package and in the second Example we will use ioutil.WriteFile function from ioutil package. Syntax os.OpenFile("test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) The OpenFile() function is present in os package and is used to open a file. The function accepts the file to be opened as argument to the function along with ...

Read More

Golang Program to open a file in read-write mode with truncating file

Akhil Sharma
Akhil Sharma
Updated on 03-May-2023 329 Views

In Golang, the functions os.openfile () and ioutil.Writefile() are used to access/open a file in read-write mode without truncating it. In this article, we will understand the functionality of these two functions using two different methods. In the first method we will use the OpenFile() function present in os package and In the second method, we will use the WriteFile() function to implement the result. Syntax os.OpenFile() The OpenFile() function is present in os package and is used to open a file. The function accepts the file to be opened as argument to the function along with ...

Read More

Golang Program to open a file in write-only mode

Akhil Sharma
Akhil Sharma
Updated on 03-May-2023 3K+ Views

In Golang, we can use OS functions os.OpenFile(), os.Create() and ioutil.WriteFile to open a write-only file. Here we are going to see three different Examples to understand the functionality of these functions. In the first program we will use the OpenFile() function while in the second and third program, we will use the writeFile() and create() function respectively. Syntax func WriteFile(filename string, data []byte, perm os.FileMode) error The WriteFile() function is present in the ioutil package and is used to write the data in the file. The function accepts the name of file in which data is to be ...

Read More

Haskell Program to Check Armstrong Number

Akhil Sharma
Akhil Sharma
Updated on 25-Apr-2023 430 Views

In Haskell we can check whether a given number is Armstrong or not using list comprehension and sum function. Armstrong numbers, also known as narcissistic numbers, are numbers such that the sum of the cubes of their digits is equal to the number itself. For example, the number 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. Algorithm Step 1 − The armstrong function calculates the sum of the cubes of the digits of n using list comprehension and the sum function, and define Step 2 − Program execution will be started from main function. ...

Read More

Haskell Program to Check Leap Year

Akhil Sharma
Akhil Sharma
Updated on 25-Apr-2023 906 Views

In Haskell, we can check whether a given year is a leap year is not using simple boolean expression. A leap year is a year that has an extra day (February 29th) compared to a normal year. For example, 2004 is a leap year. To determine if a year is a leap year or not, there are a few rules that must be followed. Algorithm Step 1 − The isLeapYear function is defined Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main ...

Read More

Haskell Program to Convert Array to Set (HashSet)

Akhil Sharma
Akhil Sharma
Updated on 25-Apr-2023 327 Views

In Haskell, we will convert Array to Set (HashSet) by using fromList, nub and foldr functions. In the first example, we are going to use ( let set = Set.fromList arr) and in the second example, we are going to use ( let set = nub arr). And in the third example, we are going to use (let set = foldr Set.insert Set.empty arr). Algorithm Step 1 − The Data.Set module is imported to work over set. Step 2 − The program execution will be started from main function. The main() function has whole control of the ...

Read More

Haskell Program to Convert File to Byte Array

Akhil Sharma
Akhil Sharma
Updated on 25-Apr-2023 546 Views

In Haskell, we will convert File to byte array by using B.readFile function along with fromIntegral and foldl function. In the first example, we are going to use ( let byteArray = B.unpack bytes) and in the second example, we are going to use (let wordArray = map fromIntegral (B.unpack bytes) :: [Word8]). And in the third example, we are going to use (let byteArray = B.foldl' (\acc byte -> acc ++ [byte]) [] bytes). Algorithm Step 1 − The Data.ByteString modules are imported. Step 2 − The program execution will be started from main function. The main() ...

Read More

Haskell Program to convert primitive types to objects

Akhil Sharma
Akhil Sharma
Updated on 25-Apr-2023 237 Views

In Haskell, we will convert primitive types to objects by using accessor functions along with getName function, constructors and record syntax. In the first example, we are going to use (getName person = name person) and in the second example, we are going to use (getName (Person name _) = name and getAge (Person _ age) = age). And in the third example, we are going to use record syntax. Algorithm Step 1 − The ‘Person’ data type is defined with two fields I.e., Name and Age. Step 2 − The getName function is defined Step 3 − ...

Read More

Haskell Program To Find The Perfect Number

Akhil Sharma
Akhil Sharma
Updated on 24-Apr-2023 619 Views

In haskell we can use list comprehension and brute-force method to find the perfect number. What is a Perfect Number? Perfect numbers are positive integers that are equal to the sum of their proper divisors. A divisor of a positive integer n is a positive integer that divides n exactly, leaving no remainder. A proper divisor is a divisor of n that is less than n itself. For example, the proper divisors of 6 are 1, 2, and 3, and the sum of these divisors is 1 + 2 + 3 = 6. Therefore, 6 is a perfect number.. Algorithm ...

Read More

Haskell Program to Check Whether a Number is Prime or Not

Akhil Sharma
Akhil Sharma
Updated on 24-Apr-2023 1K+ Views

To check whether a given number is prime or not we are going to use mod function and list comprehension method in Haskell. What is a Prime Number? A prime number is a positive integer greater than 1 that is only divisible by 1 and itself. In other words, a prime number cannot be written as the product of two smaller positive integers, except for 1 and itself. For example, the first few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29. Algorithm Step 1 − The isPrime function is defined. Step 2 ...

Read More
Showing 121–130 of 507 articles
« Prev 1 11 12 13 14 15 51 Next »
Advertisements