Programming Articles

Page 2175 of 2547

Recursive program to print formula for GCD of n integers in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 02-Nov-2021 286 Views

We are given an integer as input. The goal is to print the formula of GCD of n numbers using recursion.We know that the GCD of three numbers say a1, b1 and c1 will be gcd(a1, gcd(b1, c1)).Similarly for more than three numbers, gcd can be obtained by formula as gcd ( a1, gcd(b1, gcd(c1….., gcd(y1, z1)).ExamplesInput − Num = 4;Output − Formula is:GCD(int a3, GCD(int a2, GCD(int a1, int b1)))Input − Num = 6;Output − Formula is: GCD(int a5, GCD(int a4, GCD(int a3, GCD(int a2, GCD(int a1, int b1)))))Approach used in the below program is as followsIn this approach we are using the ...

Read More

Recursive program for prime number in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 02-Nov-2021 6K+ Views

We are given an integer as input. The goal is to find whether the input number Num is a prime or non-prime using recursion.To check if a number is prime or not, start traversing from i=2 to i

Read More

Recursive Implementation of atoi() in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 02-Nov-2021 2K+ Views

We are given a string containing a number. The goal is to find the equivalent number using the recursive atoi() method. int atoi(const char *str) converts the string argument str to an integer (type int).Example-:Input − Str[] = "58325"Output − The Equivalent decimal is :58325Explanation − The string contains the equivalent number 58325Input − Str[] = "00010"Output − The Equivalent decimal is :1Explanation − The string contains the equivalent number 10.Approach used in the below program is as followsIn this approach we are using the recursive function recurAtoi() which takes input string and its length and for each character convert it to decimal and multiply ...

Read More

Find the product of vector elements in pairs moving forward in R.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 253 Views

To find the product of vector elements in pairs moving forward, we can use prod function along with combn function.For Example, if we have a vector called V and we want to find the product of elements of V in pairs moving forward then we can use the following command −combn(V,2,prod)Example 1Following snippet creates a sample data frame −x1

Read More

How to create bar plot using ggplot2 with structure data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 1K+ Views

To create bar plot using ggplot2 with structure data, we need to set the stat argument of geom_bar function to identity. This is same for the regular data frame.For example, if we have a structured data frame called df that contains a column X for categories and a numerical column say Y for frequency then we can the bar plot for this data by using the below given command −ggplot(df,aes(X,Y))+geom_bar(stat="identity")ExampleFollowing snippet creates a sample data frame −df

Read More

How to display data frame name in ggplot2 graph title in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 685 Views

To display data frame name in ggplot2 graph title, we can use ggtitle function and pass the name of the data frame.For example, if we have a data frame called df that contains two columns say x and y and we can create a point chart between x and y then the plot with data frame name as title can be created by using the below mentioned command −ggplot(df,aes(x,y))+geom_point()+ggtitle("df")ExampleFollowing snippet creates a sample data frame −x

Read More

How to create a colored frame for ggplot2 graph in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 350 Views

To create a colored frame for ggplot2 graph, we can use theme function and set the plot.background argument to different color for rectangular element.For example, if we have a data frame called df that contains two columns say X and Y then we can create point chart between X and Y with blue colored frame of the plot using the below mentioned command −ggplot(df,aes(X,Y))+geom_point()+theme(plot.background=element_rect(colour="blue",size=3))ExampleFollowing snippet creates a sample data frame −x

Read More

How to add title to regression model using stargazer in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 1K+ Views

To add title to regression model using stargazer, we can use title argument inside stargazer function.For example, if we have a model called Reg_Model with Output as text then the title to this model using stargazer can be added by using the below mentioned command −stargazer(Reg_Model,type="text",title="Regression Model between x and y")Example 1Following snippet creates a sample data frame −x

Read More

How to delete a row from an R data frame if any value in the row is greater than n?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 6K+ Views

To delete a row from an R data frame if any value in the row is greater than n can be done by using the subsetting with single square brackets and negation operator. We will subset the values that are greater than n and then take the negation of the subset as shown in the below given examples.Example 1Following snippet creates a sample data frame −x1

Read More

How to find the sum product of two matrix by row in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 02-Nov-2021 825 Views

To find the sum product of two matrix by row in R, we can use rowSums function by passing the multiplication of the matrices.For example, if we have two matrices say Matrix1 and Matrix2 then, the sum product of these two matrices by row can be found by using the following command −rowSums(Matrix1*Matrix2)Example 1Following snippet creates a matrix −M1

Read More
Showing 21741–21750 of 25,466 articles
Advertisements