Programming Articles - Page 1169 of 3363

N’th palindrome of K digits in C++

Hafeezul Kareem
Updated on 22-Oct-2021 10:36:22

501 Views

To find the n-th palindrome of k digits, we can iterate from the first k digits number till we find the n-th palindrome number. This approach is not efficient. You can try it yourself.Now, let's see the efficient approach to find the n-th palindrome of k digits.There are two halves in the numbers. The first half is equal to the reverse of the second half.The first half of the n-th number with k digits areIf k is odd then (n - 1) + 10k/2else(n-1)+10k/2-1The second half of the n-th number with k digits will be the reverse of the first ... Read More

n-th term of series 1, 17, 98, 354…… in C++

Hafeezul Kareem
Updated on 22-Oct-2021 09:53:36

206 Views

The given series is 1, 17, 98, 354...If you clearly observe the series, you will find that the n-th number is equal to the 4 powers.Let's see the pattern. 1 = 1 ^ 4 17 = 1 ^ 4 + 2 ^ 4 98 = 1 ^ 4 + 2 ^ 4 + 3 ^ 4 354 = 1 ^ 4 + 2 ^ 4 + 3 ^ 4 + 4 ^ 4 ...AlgorithmInitialise the number N.Initialise the result to 0.Write a loop that iterates from 1 to n.Add 4th power current number to the result.Print the result.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthTerm(int n) {    int nthTerm = 0;    for (int i = 1; i

N-th term in the series 1, 11, 55, 239, 991,…in C++

Hafeezul Kareem
Updated on 22-Oct-2021 08:03:47

136 Views

The given series is 1, 11, 55, 239, 991...If you clearly observe the series, you will find that the n-th number is 4n-2n-1.AlgorithmInitialise the number N.Use the series formula to compute the n-th term.Print the result.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthTerm(int n) {    int num = pow(4, n) - pow(2, n) - 1;    return num; } int main() {    int n = 7;    cout

n-th term in series 2, 12, 36, 80, 150….in C++

Hafeezul Kareem
Updated on 22-Oct-2021 07:46:48

199 Views

The given series is 2, 12, 36, 80, 150...If you clearly observe the series, you will find that the n-th number is n2 + n3.AlgorithmInitialise the number N.Use the series formula to compute the n-th term.Print the result.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthTerm(int n) {    return (n * n) + (n * n * n); } int main() {    int n = 7;    cout

N-th root of a number in C++

Hafeezul Kareem
Updated on 22-Oct-2021 07:38:31

934 Views

You are given the N-th root and the result of it. You need to find the number such that numberN = result.Let's see some examples.Input result = 25 N = 2Output 5The 52 = 25. Hence the output in the above example is 5.Inputresult = 64 N = 3Output4 The 43 = 64. Hence the output in the above example is 4.AlgorithmImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthRoot(int result, int n) {    int i = 1;    while (true) {       if (pow(i, n) == result) {          return i;       }       i += 1;    } } int main() {    int result = 64, N = 6;    cout

N-th polite number in C++

Hafeezul Kareem
Updated on 22-Oct-2021 06:54:46

254 Views

A polite number is a positive number that can be written as the sum of 2 or more consecutive positive numbers.The series of polite numbers are3 5 6 7 9 10 11 12 13 14...There exists a formula to find the n-th polite number. The formula is n + log2(n + log2(n)). The default log computes with base e. We need to compute using base 2. Divide the default log result with log(2) to get the value of log with base e.AlgorithmAlgorithm to the n-th polite number is straightforward.Initialise the number N.Use the above formula to compute the n-th polite ... Read More

n-th number with digits in {0, 1, 2, 3, 4, 5} in C++

Hafeezul Kareem
Updated on 22-Oct-2021 06:33:37

367 Views

The numbers formed with the digits {0, 1, 2, 3, 4, 5} are0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, etc.., We can form the above sequence using the first 6 digits. Let's see an example of the formation of numbers. 1 * 10 + 0 = 10 1 * 10 + 1 = 11 1 * 10 + 2 = 12 1 * 10 + 3 = 13 1 * 10 + 4 = 14 1 * 10 + 5 = 15Similarly, apply for the number 2, 3, 4, ... Read More

n-th number whose sum of digits is ten in C++

Hafeezul Kareem
Updated on 22-Oct-2021 06:08:19

392 Views

The numbers whose digits sum is equal to 10 are19, 28, 37, 46, 55, 64, 73, 82, 91, etc.., If you observe the series, each number is incremented by 9. There are numbers in the above sequence whose digits sum does not equal 10 while incrementing by 9. But, you will get all the numbers whose digits sum is equal to 10.So, we can have a loop that increments by 9 and checks for digits sum and finds the n-th number. Let's see some examplesInputs 3 7Outputs 37 73AlgorithmInitialise the number nInitialise a counter to 0.Write a loop that iterates ... Read More

N-th multiple in sorted list of multiples of two numbers in C++

Hafeezul Kareem
Updated on 22-Oct-2021 05:54:52

241 Views

You are given three numbers. You need to find the n-th multiple from the multiples of the first two numbers. Let's see an example to understand it more clearly.Input x = 2 y = 3 n = 7Output 10The first n ****multiples of 2 are 2 4 6 8 10 12 14The first n ****multiples of 3 ****are 3 6 9 12 15 18 21If combine both multiples and sort them we get 2 3 4 6 8 9 10 12 14 15 18 21 and the nth number from the list is 10.AlgorithmInitialise a vector to store all the ... Read More

N digit numbers divisible by 5 formed from the M digits in C++

Hafeezul Kareem
Updated on 21-Oct-2021 12:57:36

229 Views

We have given a number N along with an array of M digits. Our job is to find the number of n digit numbers formed from the given M digits that are divisible by 5.Let's see some examples to understand the problem inputs and outputs.In − N = 2 M = 3 arr = {5, 6, 3}Out − 2There are 2 N digit numbers 35 and 65 possible that are divisible by 5. Let's see another example.Input − N = 1 M = 7 arr = {2, 3, 4, 5, 6, 7, 8}Output − 1There is only 1 number with ... Read More

Advertisements