Found 7347 Articles for C++

C++ program to find Nth term of the series 0, 2, 4, 8, 12, 18…

sudhir sharma
Updated on 13-Mar-2021 13:20:47

303 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 0, 2, 4, 8, 12, 18…Let’s take an example to understand the problem, InputN = 5Output12Solution ApproachA simple approach to solve the problem is the formula for the Nth term of the series. For this, we need to observe the series and then generalise the Nth term.The formula of Nth term isT(N) = ( N + (N - 1)*N ) / 2Program to illustrate the working of our solution, Example Live Demo#include using namespace std; int calcNthTerm(int N) ... Read More

C++ program to find Nth term of series 1, 4, 15, 72, 420…

sudhir sharma
Updated on 13-Mar-2021 13:19:57

329 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1, 4, 15, 72, 420…Let’s take an example to understand the problem, InputN = 4Output72Solution ApproachA simple approach to solve the problem is the formula for the Nth term of the series. For this, we need to observe the series and then generalise the Nth term.The series can be seen as the product of factorial and a some variables, 1, 4, 15, 72, 420… 1!*(X1), 2!*(X2), 3!*(X3), 4!*(X4), 5!*(X5)... 1*(1), 2*(2), 6*(5/2), 24*(3), 120*(7/2)...Here, the series of product ... Read More

Find Nth term (A matrix exponentiation example) in C++

sudhir sharma
Updated on 13-Mar-2021 13:17:32

151 Views

In this problem, we are given an integer N and a recursive function that given Nth term as a function of other terms. Our task is to create a program to Find Nth term (A matrix exponentiation example).The function isT(n) = 2*( T(n-1) ) + 3*( T(n-2) ) Initial values are    T(0) = 1 , T(1) = 1Let’s take an example to understand the problem, InputN = 4Output41ExplanationT(4) = 2* (T(3)) + 3*(T(2)) T(4) = 2* ( 2*(T(2)) + 3*(T(1)) ) + 3*( 2* (T(1)) + 3*(T(0)) ) T(4) = 2*( 2*(2* (T(1)) + 3*(T(0))) + 3*(1) ) + ... Read More

Find Nth positive number whose digital root is X in C++

sudhir sharma
Updated on 13-Mar-2021 13:15:37

104 Views

In this problem, we are given two integer values N and X. Our task is to create a program to Find Nth positive number whose digital root is X.Digital Root (X) is a single digit positive number which is found by adding digits of N recursively adding digits, till the sum becomes single digit.Let’s take an example to understand the problem, InputN = 5, X = 4Output40Solution ApproachA simple way to solve the problem is by counting the numbers with a digital root is X. For this, we will start from 1 and then check if the current number’s digital ... Read More

C++ program to find Nth number of the series 1, 6, 15, 28, 45, …..

sudhir sharma
Updated on 13-Mar-2021 13:10:39

234 Views

In this problem, we are given an integer value N. Our task is to create a program to Find Nth number of the series 1, 6, 15, 28, 45, …In the series, every element is 2 less than the mean of the previous and next element.Let’s take an example to understand the problem, InputN = 5Output45Solution ApproachThe Nth term of the series 1, 6, 15, 28, 45, … can be found using the formula, TN = 2*N*N - NProgram to illustrate the working of our solution, Example Live Demo#include using namespace std; #define mod 1000000009 int calcNthTerm(long n) {   ... Read More

Find nth Hermite number in C++

sudhir sharma
Updated on 13-Mar-2021 13:09:30

75 Views

In this problem, we are given an integer value N. Our task is to create a program to Find nth Hermite number.Hermite Number is a number is the value of hermite number when there are 0 arguments.Nth hermite Number is HN = (-2) * (N - 1) * H(N-2) The base values are H0 = 1 and H0 = 0.The hermite sequence is − 1, 0, -2, 0, 12, 0, -120, 0, 1680, 0….Let’s take an example to understand the problem, InputN = 7Output0InputN = 6Output-120Solution ApproachA simple solution to the problem is using the formula for hermite number. This ... Read More

Find Next Sparse Number in C++

sudhir sharma
Updated on 13-Mar-2021 13:06:17

293 Views

In this problem, we are given an integer value N. our task is to create a program to find the next spares Number.Sparse Number is a special type of number whose binary conversion does not contain any adjacent 1’s.Example: 5(101) , 16(10000)Problem Description − For the given number N, we need to find the smallest number greater than N which is a sparse number.Let’s take an example to understand the problem, InputN = 7Output8ExplanationThe binary of 8 is 1000 which makes it the smallest sparse number greater than n.Solution ApproachA simple solution to the problem is checking for all numbers ... Read More

Find next Smaller of next Greater in an array in C++

sudhir sharma
Updated on 13-Mar-2021 13:03:33

258 Views

In this problem, we are given an array arr[] consisting of n integer values. Our task is to Find the next Smaller of the next Greater in an array.Problem Description − We will find an element greater than the current element in the array and then we will be finding the element in the array which is smaller than this greater element. And if no next smaller or next greater element exists in the array return -1.Let’s take an example to understand the problem, Inputarr[] = {4, 2, 8, 3, 9, 1}Output{3, 3, 1, 1, -1, -1}ExplanationArray of next greater ... Read More

Find next right node of a given key in C++

sudhir sharma
Updated on 13-Mar-2021 13:00:02

169 Views

In this problem, we are given a binary Tree BT and a key value. Our task is to Find next right node of a given key.Binary Tree is a special data structure used for data storage purposes.Let’s take an example to understand the problem, Inputkey = 4Output5ExplanationThe element next to the node 4 is 5.Solution ApproachA simple solution to the problem is by traversing the binary tree using the level order traversal. And for the given key value, we will check if there exists an node next to node at the same level in the traversal. If Yes, return the ... Read More

Find next palindrome prime in C++

sudhir sharma
Updated on 13-Mar-2021 12:37:34

203 Views

In this problem, we are given an element N. We need to find the next palindrome prime.Problem Description − We need to find the smallest prime number which is also a palindrome number, greater than N.Palindrome Number is a number in which the numbers are the same in both directions.Prime Number is a number if its only factors are 1 and itself.Let’s take an example to understand the problem, InputN = 12Output101ExplanationThe series of palindromes greater than 12 are 22, 33, 44, 55, 66, 77, 88, 99, 101… out of these the smallest palindrome is 101.Solution ApproachA simple solution to ... Read More

Advertisements