Found 7347 Articles for C++

Natural Numbers in C++ Program

Hafeezul Kareem
Updated on 23-Oct-2021 07:43:13

2K+ Views

Numbers that are greater than 0 are called natural numbers. The natural number are1, 2, 3, 4, 5, 6, 7...AlgorithmInitialise the number n.Write a loop that iterates from 1 to n.Print the numbers.Increment the iterative variable.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; void printNaturalNumbers(int n) {    for (int i = 1; i

N’th Smart Number in C++

Hafeezul Kareem
Updated on 23-Oct-2021 06:31:40

162 Views

A smart number is a number that contains at least three distinct prime factors. You are given a number N. Find the n-th smart number.The smart number series are30, 42, 60, 66, 70, 78...AlgorithmInitialise the number N.Initialise the count to 0.Write a function that checks whether the given number is prime or not.Write a function that checks whether the number is smart or not.Write a loop that iterates from 30 as first smart number is 30.Check whether the current number is smart number or not using the prime number function.Increment the count by 1 when you find a smart number.Return ... Read More

n’th Pentagonal Number in C++

Hafeezul Kareem
Updated on 22-Oct-2021 11:04:32

265 Views

In this tutorial, we are going to write a program that finds the n-th pentagonal number.A pentagonal number is a number represented as dots or pebbles arranged in the shape of a regular polygon. Refer to the wiki for better understanding.The n-th pentagonal number is (3 * n * n - n) / 2.The series of pentagonal numbers are 1, 5, 12, 22, 35, 51, 70, 92...AlgorithmInitialise the number n.Use the formula to find the n'th pentagonal number.Print the resultant number.ImplementationFollowing is the implementation of the above algorithm in C++#include using namespace std; int getNthPentagonalNumber(int n) {   ... Read More

N’th palindrome of K digits in C++

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

305 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

109 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

79 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

119 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

641 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

122 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

245 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

Advertisements