Found 7347 Articles for C++

Find number of days between two given dates in C++

sudhir sharma
Updated on 15-Mar-2021 10:52:41

4K+ Views

In this problem, we are given two arrays date1[] and date2 consisting of 3 integers which denote the DD-MM-YYYY of daes. Our task is to find the number of days between two given dates.Let’s take an example to understand the problem, Inputdate1[] = {13, 3, 2021}, date2[] = {24, 5, 2023}Output802ExplanationThe difference is 2 years , 2 months (3 - 5) and 11 days.2*356 + (30 + 31) + 11 = 802Solution ApproachA simple solution to the problem is by looping, starting from the start date date1 to date2 counting the number of days. And returning the value. This approach ... Read More

Find number from its divisors in C++

sudhir sharma
Updated on 15-Mar-2021 10:46:52

162 Views

In this problem, we are given an array divisors[] consisting of N integers which are the divisors of a number Num. Our task is to find the number from its divisors.The divisor array does not include 1 and the number.Let’s take an example to understand the problem, Inputdivisors[] = {3, 25, 5, 15}Output75ExplanationThe number 75 has divisors {3, 25, 5, 15}Solution ApproachTo solve the problem, we need to find the number Num using the smallest and largest divisors of the number.Num = smallest * largestFor this, we need to sort the array divisors[] and then find the product of elements ... Read More

C++ program to find Nth term of the series 5, 13, 25, 41, 61…

sudhir sharma
Updated on 15-Mar-2021 10:45:14

157 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 5, 13, 25, 41, 61, …Let’s take an example to understand the problem,InputN = 5Output61ExplanationThe series is − 5, 13, 25, 41, 61…Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The Nth term is given by,Nth term = ( (N*N) + ((N+1)*(N+1)) )Program to illustrate the working of our solution,Example Live Demo#include using namespace std; int calcNthTerm(int N) {    return ( ( (N + 1)*( N + 1) ) + (N*N) ) ; } int main() {    int N = 7;    cout

C++ program to find nth term of the series 5, 2, 13 41,...

sudhir sharma
Updated on 15-Mar-2021 10:43:50

169 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 5, 2, 19, 13, 41, 31, 71, 57…Let’s take an example to understand the problem, InputN = 5Output41ExplanationThe series is − 5, 2, 19, 13, 41, …Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The series has different formulas for even and odd values.The Nth term is given by, Nth term = (N-1)^2 + N, if N is even i.e N%2 == 0 Nth term ... Read More

C++ program to find Nth term of the series 3, 14, 39, 84…

sudhir sharma
Updated on 15-Mar-2021 10:42:43

138 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 3, 14, 39, 84…Let’s take an example to understand the problem,InputN = 4Output84Explanation4th term − ( (4*4*4) + (4*4) + 4 ) = 64 + 16 + 4 = 84Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The formula for,Nth term = ( (N*N*N) + (N*N) + (N))Program to illustrate the working of our solution,Example Live Demo#include using namespace std; int calcNthTerm(int N) {    return ( (N*N*N) + (N*N) + (N) ); } int main() {    int N = 6;    cout

C++ program to find Nth term of the series 1, 8, 54, 384…

sudhir sharma
Updated on 15-Mar-2021 10:41:39

108 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1,8, 54, 384 ...Let’s take an example to understand the problem,InputN = 4Output384Explanation4th term − (4 * 4 * (4!) = 384Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The formula for,Nth term = ( N * N * (N !) )Program to illustrate the working of our solution,Example Live Demo#include using namespace std; int calcFact(int N) {    int fact = 1;    for (int i = 1; i

C++ program to find Nth term of the series 1, 6, 18, 40, 75, ….

sudhir sharma
Updated on 15-Mar-2021 10:40:07

190 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1,6, 18, 40, 75 ...Let’s take an example to understand the problem,InputN = 4Output40Explanation4th term − (4 * 4 * 5 ) / 2 = 40Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The formula for,Nth term = ( N * N * (N + 1) ) / 2Program to illustrate the working of our solution,Example Live Demo#include using namespace std; int calcNthTerm(int N) {    return ( (N*N*(N+1))/2 ); } int main() {    int N = 5;    cout

C++ program to find Nth term of the series 1, 5, 32, 288 …

sudhir sharma
Updated on 15-Mar-2021 10:38:54

137 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1,5, 32, 288 ...Let’s take an example to understand the problem,InputN = 4Output288Explanation4th term − (4^4) + (3^3) + (2^2) + (1^1) = 256 + 27 + 4 + 1 = 288Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The formula for,Nth term = ( N^N ) + ( (N-1)^(N-1) ) + … + ( 2^2 ) + ( 1^1 )Program to illustrate the working of our solution,Example Live Demo#include using namespace std; int calcNthTerm(int N) {    if (N

C++ program to Find Nth term of the series 1, 1, 2, 6, 24…

sudhir sharma
Updated on 15-Mar-2021 10:37:36

424 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1,1, 2, 6, 24, ...Let’s take an example to understand the problem,InputN = 7Output720ExplanationThe series is − 1, 1, 2, 6, 24, 120, 720Solution ApproachA simple approach to solve the problem is by using the general formula for the nth term of the series. The formula for,Nth term = (N−1)!Program to illustrate the working of our solution,Example Live Demo#include using namespace std; int calcNthTerm(int N) {    if (N

C++ program to find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 …

sudhir sharma
Updated on 15-Mar-2021 10:36:19

130 Views

In this problem, we are given an integer N. Our task is to create a program to Find Nth term of series 1, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8…Let’s take an example to understand the problem, InputN = 7Output4Solution ApproachA simple approach to solve the problem is using a loop to find the term at the nth position. The terms will be updated by doubling after each iteration. And adding it to the term counter.Program to illustrate the working of our solution, Example#include using namespace std; int calcNthTerm(int N) {   ... Read More

Advertisements