Programming Articles

Page 1298 of 2547

How to multiply each element of a numerical vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

Sometimes we want to determine the multiplication of all the elements of a vector just like the sum. This might be required to test the changes in the mathematical operations that can be applied to a numerical vector. In base R, we have prod function which works same as sum but give us the multiplication of all the elements of a vector.Example> v1 v1Output[1] 1 2 3 4 5> prod(v1)Output[1] 120 Example> v2 v2Output[1] -0.500466629 0.394771317 0.575743107 0.026982141 0.812697502 [6] 0.995708241 2.198243938 -0.008609976 -0.931337300 -0.073743225> prod(v2)Output[1] 3.228448e-06Example> v3 v3Output[1] 1.7328245888 -0.5772304935 2.5161349689 3.0401656274 0.1669773313 [6] -0.0001252235 0.7649984733 2.4901543043 1.5618729422 2.4392364199> ...

Read More

How to check if a string is a valid keyword in Java?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

To check if a string is a valid keyword in Java, the code is as follows −Exampleimport java.util.*; public class Demo{    static boolean valid_identifier(String my_str, int n){       if (!((my_str.charAt(0) >= 'a' && my_str.charAt(0) = 'A' && my_str.charAt(1) = 'a' && my_str.charAt(i) = 'A' && my_str.charAt(i) = '0' && my_str.charAt(i)

Read More

How to combine two vectors while replacing the NA values with the values in the other vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 794 Views

Sometimes we have vectors with NA values, also there might be a situation that one of vector having an NA at a position and the other vector has the numerical values at the same position. For example, 1, 2, NA and 1, 2, 3. In this case, we might want to combine these two vectors to make a single vector. This can be done by using coalesce function of dplyr package.Example> library(dplyr) > x1 x1Output[1] NA 4 NA 1 2 NA 4 1 4 1 2 3 1 4 2 2 NA 2 2 1Example> y1 y1Output[1] 1 2 3 ...

Read More

Complex Numbers in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 7K+ Views

Complex numbers are those that have an imaginary part and a real part associated with it. They can be added and subtracted like regular numbers. The real parts and imaginary parts are respectively added or subtracted or even multiplied and divided.Examplepublic class Demo{    double my_real;    double my_imag;    public Demo(double my_real, double my_imag){       this.my_real = my_real;       this.my_imag = my_imag;    }    public static void main(String[] args){       Demo n1 = new Demo(76.8, 24.0),       n2 = new Demo(65.9, 11.23),       temp;       temp ...

Read More

Find the largest area rectangular sub-matrix whose sum is equal to k in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 350 Views

Suppose we have a 2D matrix mat and a value K, we have to find the longest rectangular submatrix whose sum is same as K.So, if the input is like28-56-778-311-1443-43110And K = 9then the output will be Top-Left point is (1, 0) and Bottom-Right point is (3, 2).-77811-144-431To solve this, we will follow these steps −MAX := 100Define a function sum_k(), this will take one array arr, start, end, n, k, Define one mapsum := 0, maximum_length := 0for initialize i := 0, when i < n, update (increase i by 1), do −sum := sum + arr[i]if sum is ...

Read More

How to add a citation in a plot created by using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

A footnote is generally used to give references to a document, text or image and it is called citation. It helps the reader or viewer to check out the original source using the new text or image is generated. If we want to give citation to a plot in R using ggplot2 package then we can add labs that has caption option to add the footnotes.ExampleConsider the below data frame −> set.seed(1) > x y df dfOutput      x       y 1 0.8735462 4.0117812 2 1.6836433 2.8898432 3 0.6643714 1.8787594 4 3.0952808 0.2853001 5 1.8295078 3.6249309 6 ...

Read More

Sum of upper triangle and lower triangle in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 1K+ Views

In this problem, we are given a matrix. Our task is to create a program to print the sum of upper triangle and lower triangle.Lower triangleM00                     0             0       …        0 M10                     M11               0       …        0 M20                     M21               M22      …     ...

Read More

Find the largest multiple of 3 from array of digits - Set 2 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 236 Views

Suppose we have an array of different digits; we have to find the largest multiple of 3 that can be generated by concatenating some of the given digits in that array in any order. The answer may be very large so make it as string. If there is no answer return an empty string.So, if the input is like [7, 2, 8], then the output will be 87.To solve this, we will follow these steps −Define one 2D array d, there will be three rowssort the array digitssum := 0for initialize i := 0, when i < size of digits, ...

Read More

How to find the column and row indices of values in a matrix using which function in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To find the row and column indices of values in a matrix, we cannot simply use which function because it returns the index based on sequence of the numbers in the matrix. For example, if we have a matrix M as below −1 2 3 4 1 6 7 8 1Now if we try to find the index using which(M==1) then it will return 1 5 9Because 1 is placed at 1, 5 and 9.Hence, we need to use arr.ind = TRUE so that the matrix can be read as an array by which function.ExampleConsider the below matrix −> M ...

Read More

Precision Handling in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 664 Views

Let us see how precisions are handled in Java −Exampleimport java.io.*; import java.lang.*; public class Demo{    public static void main(String[] args){       double my_val = 34.909;       System.out.println("The formatted value of 34.909 is ");       System.out.println(String.format("%.7f", my_val));       double my_val_2 = 12.56;       System.out.println("The formatted value of 12.56 is ");       System.out.println(String.format("%.9f", my_val_2));    } }OutputThe formatted value of 34.909 is 34.9090000 The formatted value of 12.56 is 12.560000000A class named Demo contains the main function, where a double valued integer is declared, and it is formatted ...

Read More
Showing 12971–12980 of 25,466 articles
Advertisements