Found 33676 Articles for Programming

Haskell Program to get the numerator from a rational number

Akhil Sharma
Updated on 13-Mar-2023 14:53:00

230 Views

In Haskell, we can use numerator, div, quot and gcd functions to find the numerator from a given rational number. In the first example we are going to use numerator (r) function and in the second example, we are going to use (n `div` gcd n d) function. And in third example, we are going to use (numerator r `quot` gcd (numerator r) (denominator r)) function. Algorithm Step 1 − The Data.Ratio module is imported to use numerator function. Step 2 − Program execution will be started from main function. The main() function has whole control of the program. ... Read More

Haskell Program to find the LCM using library function

Akhil Sharma
Updated on 13-Mar-2023 14:51:59

152 Views

In Haskell, we can use GCD function to find the LCM of the given number. In the first example we are going to use (a * b) `div` (gcd a b) function and in the second example, we are going to use foldl' (\x y -> (x*y) `div` (gcd x y)) function. In third example, we are going to use gcd' b (a `mod` b) function. Algorithm Step 1 − The lcm function is defined using gcd function as, lcm a b = (a * b) `div` (gcd a b). Step 2 − Program execution will be started ... Read More

Haskell Program to find the GCD using library function

Akhil Sharma
Updated on 13-Mar-2023 14:50:14

236 Views

In Haskell, we will find the GCD using library function like gcd, div function and recursion. In the first example, we are going to use gcd (a b) function and in the second example, we are going to use (a `div` b) function. In third example, we are going to use recursion. Method 1: Finding the GCD using gcd function In this method, the gcd function takes two integers as input and returns the greatest common divisor of these two numbers. This function is defined in the Prelude library. Algorithm Step 1 − Program execution will be started from ... Read More

Haskell Program to check the given number is an EVEN number using library function

Akhil Sharma
Updated on 13-Mar-2023 14:48:37

1K+ Views

In Haskell, we can use library function like mod, even and quot to check whether, the given number is an EVEN number or not. In this article we are going to have examples that are using n `mod` 2 == 0 even function as well as n `quot` 2 == 0 function. Algorithm Step 1 − The isEven function is defined using mod function as, isEven n = n `mod` 2 == 0. 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

Java Program to Encode a Message Using Playfair Cipher

Pranay Arora
Updated on 10-Mar-2023 11:31:25

3K+ Views

Encrypting is the task of transforming information into an unreadable format or cipher text. It is usually done to protect the confidentiality of information. There are many ways and algorithms to encrypt data. One such example is the Playfair cipher algorithm. In this article, we are going to see how to write a Java program to encode a message using Playfair cipher. Playfair cipher makes use of a 5X5 grid or matrix and a set of pre-defined rules. To encrypt, we require a key and the plain text to be encrypted. Steps Now let us see the steps to encrypt ... Read More

Java Program to Empty an ArrayList

Pranay Arora
Updated on 24-Dec-2024 17:55:45

613 Views

ArrayLists in Java are a dynamic array whose size can grow or shrink as elements are added or deleted from it. It is a part of the java.util package and is a very commonly used collection. An ArrayList is pretty much like an array as it stores elements of the same or homogeneous type in a contiguous block of memory. In this article, we are going to see the different ways to empty an ArrayList in Java. There are mainly 4 approaches to empty an ArrayList in Java and are as follows − Using the Naïve ... Read More

Java Program to Display Numbers and Sum of First N Natural Numbers

Shriansh Kumar
Updated on 16-Aug-2024 07:34:54

2K+ Views

Natural numbers are all positive integers or whole numbers that range between 1 to infinity. In this article, we will see how to find the sum of the first N natural numbers in Java, where N is the integer up to which we need to add all the numbers starting from 1. Example Scenario: Input: num = 5 Output: sum = 15 sum = 1 + 2 + 3 + 4 + 5 Using a for loop In this approach, initialize a variable with 0 to store the sum. Then, run a for loop that goes from ... Read More

Java program to display Floyd\'s triangle

Pranay Arora
Updated on 18-Oct-2024 11:57:12

1K+ Views

In this article, we are going to see how to display Floyd’s Triangle using Java. Floyd's triangle is a popular right-angled triangular array consisting of natural numbers. It is formed by starting with the number 1 at the top of the triangle, and then incrementing each subsequent number by 1 as you move down the rows of the triangle. The first row contains only 1 number which is 1 itself and each subsequent row contains 1 more number than the previous row. The triangle has n rows where n can be any positive whole number. The total number of values in ... Read More

Java program to demonstrate the call by value

Pranay Arora
Updated on 11-Nov-2024 19:17:11

822 Views

In programming, functions often require parameters to be passed to them to be called or invoked. There are 2 ways to call functions, the 1st being call by Reference and the 2nd being call by value. In this article, we are going to demonstrate call by value in Java. Call by value is a method in which the value of the argument is passed as a copy to the function, hence any change made to this argument inside the function will not affect the original value of the argument beyond the scope of this function. To help understand these concepts ... Read More

Pandas series Vs. single-column DataFrame

Premansh Sharma
Updated on 10-Mar-2023 14:09:06

12K+ Views

Introduction This article compares and contrasts Python's Pandas library's single-column DataFrames and Pandas Series data structures. The goal of the paper is to clearly explain the two data structures, their similarities and differences. To assist readers in selecting the best alternative for their particular use case, it contains comparisons between the two structures and practical examples on aspects like data type, indexing, slicing, and performance. The essay is appropriate for Python programmers at the basic and intermediate levels who are already familiar with Pandas and wish to get a deeper grasp of these two key data structures. What is Pandas? ... Read More

Advertisements