Found 7197 Articles for C++

Mersenne Prime Number in C++.

Narendra Kumar
Updated on 31-Oct-2019 05:54:02

671 Views

DescriptionIn mathematics, a Mersenne prime is a prime number that is one less than a power of two. That is, it is a prime number of the form Mn = 2n − 1 for some integer n.Write a C++ program to print all Mersenne Primes smaller than an input positive integer n.The exponents n which give Mersenne primes are 2, 3, 5, 7, ... and the resulting Mersenne primes are 3, 7, 31, 127Algorithm1. Generate all the primes less than or equal to the given number n 2. Iterate through all numbers of the form 2n-1 and check if they ... Read More

Find the value of ln(N!) using Recursion using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:39:33

192 Views

Suppose we have a number N, our task is to find ln(N!) using recursion. ln() is basically log base e. To solve this we can use this formula −$$\ln\lgroup N!\rgroup=\ln\lgroup N*\lgroup N-1\rgroup *\lgroup N-2\rgroup *\dotsm*2*1\rgroup=\ln\lgroup N\rgroup+\ln\lgroup N+1\rgroup+\dotsm+\ln\lgroup 1\rgroup$$Example Live Demo#include #include using namespace std; double factLog(int n) {    if (n

Find the unit place digit of sum of N factorials using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:37:53

189 Views

Here we will see how to get the unit place digit of the sum of N factorials. So if N is 3, then after getting sum, we will get 1! + 2! + 3! = 9, this will be the result, for N = 4, it will be 1! + 2! + 3! + 4! = 33. so unit place is 3. If we see this clearly, then as the factorials of N > 5, the unit place is 0, so after 5, it will not contribute to change the unit place. For N = 4 and more, it will ... Read More

Find the frequency of a number in an array using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:35:38

317 Views

Suppose we have an array. There are n different elements. We have to check the frequency of one element in the array. Suppose A = [5, 12, 26, 5, 3, 4, 15, 5, 8, 4], if we try to find the frequency of 5, it will be 3.To solve this, we will scan the array from left, if the element is the same as the given number, increase the counter, otherwise go for the next element, until the array is exhausted.Example Live Demo#include using namespace std; int countElementInArr(int arr[], int n, int e) {    int count = 0;    for(int i = 0; i

Find the frequency of a digit in a number using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:33:53

2K+ Views

Here we will see how to get the frequency of a digit in a number. Suppose a number is like 12452321, the digit D = 2, then the frequency is 3.To solve this problem, we take the last digit from the number, then check whether this is equal to d or not, if so then increase the counter, then reduce the number by dividing the number by 10. This process will be continued until the number is exhausted.Example Live Demo#include using namespace std; int countDigitInNum(long long number, int d) {    int count = 0;    while(number){       if((number ... Read More

Find the first repeated character in a string using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:32:11

2K+ Views

Suppose we have a string; we have to find the first character that is repeated. So is the string is “Hello Friends”, the first repeated character will be l. As there are two l’s one after another.To solve this, we will use the hashing technique. Create one hash table, scan each character one by one, if the character is not present, then insert into a hash table, if it is already present, then return that character.Example Live Demo#include #include using namespace std; char getFirstRepeatingChar(string &s) {    unordered_set hash;    for (int i=0; i

Find the factorial of a number in pl/sql using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:29:26

9K+ Views

In this section, we will see how to find factorial of a number using the PL/SQL. In PL/SQL code, some group of commands is arranged within a block of the related declaration of statements.Factorial of a number is basically multiplication of all integers from 1 to n, where n is the given number. So n! = n * (n – 1) * (n – 2) * … * 2 * 1.Example (PL/SQL) Live DemoDECLARE    fact number :=1;    n number := &1; BEGIN    while n > 0 loop       fact:=n*fact;       n:=n-1;    end loop;    dbms_output.put_line(fact); END;OutputConsider 5 has given 120

Find sum of odd factors of a number using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:27:31

339 Views

In this section, we will see how we can get the sum of all odd prime factors of a number in an efficient way. There is a number say n = 1092, we have to get all factors of this. The prime factors of 1092 are 2, 2, 3, 7, 13. The sum of all odd factors is 3+7+13 = 23. To solve this problem, we have to follow this rule −When the number is divisible by 2, ignore that factor, and divide the number by 2 repeatedly.Now the number must be odd. Now starting from 3 to square root ... Read More

Find sum of even factors of a number using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:22:47

565 Views

In this section, we will see how we can get the sum of all even prime factors of a number in an efficient way. There is a number say n = 480, we have to get all factors of this. The prime factors of 480 are 2, 2, 2, 2, 2, 3, 5. The sum of all even factors is 2+2+2+2+2 = 10. To solve this problem, we have to follow this rule −When the number is divisible by 2, add them into the sum, and divide the number by 2 repeatedly.Now the number must be odd. So we will ... Read More

Find original array from encrypted array (An array of sums of other elements) using C++.

Arnab Chakraborty
Updated on 30-Oct-2019 06:13:30

610 Views

Let us consider we have an array of integers, that array is encrypted array, Suppose the array is A = [10, 14, 12, 13, 11], the original array is B = [5, 1, 3, 2, 4], we can see that each element at index I of A follows this rule: A[i] = sum of all elements at position j in B[j], where I ≠ j. Our task is to find an original array from the encrypted one.The task is based on arithmetic observation. Suppose the array is of size 4, original array B has four elements B = [a, b, ... Read More

Advertisements