Found 7347 Articles for C++

First triangular number whose number of divisors exceeds N in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:11:43

96 Views

In this tutorial, we are going to find a triangular number whose number of divisors are greater than n.If the sum of natural numbers at any point less than or equal to n is equal to the given number, then the given number is a triangular number.We have seen what triangular number is. Let's see the steps to solve the problem.Initialize the numberWrite a loop until we find the number that satisfies the given conditions.Check whether the number is triangular or not.Check whether the number has more than n divisors or not.If the above two conditions are satisfied then print ... Read More

First non-repeating character using one traversal of string in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:09:45

1K+ Views

In this tutorial, we are going to learn how to find the first non-repeating character in the given string. Let's see an example.Input −tutorialspointOutput −uLet's see the steps to solve the problem.Initialize the string.Initialize a map char and array to store the frequency of the characters in the string.Iterate over the string.Find the frequency of each character and store them in the map.Store the index of the character as well.Iterate over the character frequencies in the map.Print the first character with the frequency 1.ExampleLet's see the code.#include #include using namespace std; void findDistinctCharacters(string random_string) {    // initializing ... Read More

First N natural can be divided into two sets with given difference and co-prime sums in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:07:42

55 Views

In this tutorial, we have to find whether the natural numbers from 1 to n is divided into two halves or not. It has to satisfy the following conditions.The absolute difference between the two series sum should be m.And the GCD of two sums should be 1 i.e.., co-primes.The sum of first n natural numbers is (n*(n+1))/2. We can find the sumOne and sumTwo as we have total sum and difference m. See the below equations.sumOne + sumTwo = (n*(n+1))/2 sumOne - sumTwo = mExampleCheck whether the absolute sum is equal to m or not. And then check for GCD. Live ... Read More

First digit in product of an array of numbers in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:06:13

112 Views

In this tutorial, we are going to learn how to find first digit of the product of an array.Let's see the steps to solve the problem.Initialize the array.Find the product of the elements in the array.Divide the result until it's less than 10.Print the single-digitExampleLet's see the code. Live Demo#include using namespace std; int productOfArrayDigits(int arr[], int n) {    int product = 1;    for (int i = 0; i < n; i++) {       product *= arr[i];    }    return product; } int firstDigitOfNumber(int n) {    while (n >= 10) {       n /= 10;    }    return n; } int main() {    int arr[] = { 1, 2, 3, 4, 5, 6 };    cout

First digit in factorial of a number in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:04:26

96 Views

In this tutorial, we are going to write a program the finds the first digit of a factorial. Let's see an example.Input − 7Output − 5Let's see the steps to solve the problem.Initialize the numberFind the factorial of the number.Divide the number until it becomes a single digit.ExampleLet's see the code. Live Demo#include using namespace std; void findFirstDigitOfFactorial(int n) {    long long int fact = 1;    for (int i = 2; i = 10) {       fact = fact / 10;    }    cout

Finding the vertex, focus and directrix of a parabola in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:02:50

126 Views

In this tutorial, we are going to learn how to find the vertex, focus, and directrix of a parabola. We are given constants of the parabola equation x, y, and z.There are straightforward formulas to find the vertex, focus, and directrix. Let's them.Vertex − (-y/2x, 4xz-y^2/4x)Focus − (-y/2x, 4xz-y^2+1/4x)Directrix − z-(y^2+1)4xExampleLet's see the code. Live Demo#include using namespace std; void findParabolaProperties(float x, float y, float z) {    cout

Finding the Parity of a number Efficiently in C++

Hafeezul Kareem
Updated on 29-Dec-2020 11:01:23

2K+ Views

In this tutorial, we are going to write a program that finds the parity of a number.We can find the parity of a number efficiently by performing the following operations using xor and right-shift operators.int b; b = n ^ (n >> 1); b = b ^ (b >> 2); b = b ^ (b >> 4); b = b ^ (b >> 8); b = b ^ (b >> 16);If the last bit of the result is 1 then it's an odd parity else even parity.ExampleLet's see the code. Live Demo#include using namespace std; void findParity(int n) {   ... Read More

Finding sum of digits of a number until sum becomes single digit in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:59:58

7K+ Views

In this tutorial, we are going to write a program that sums digits of the given number until it becomes a single digit. Let's see an example.Input −4543Output −7Let's see the steps to solve the problem.Initialize a number.Initialize the sum to 0.Iterate until the sum is less than 9.Add each digit of the number to the sum using modulo operatorPrint the sumExampleLet's see the code. Live Demo#include using namespace std; void findTheSingleDigit(int n) {    int sum = 0;    while(n > 0 || sum > 9) {       if(n == 0) {          n = sum;          sum = 0;       }       sum += n % 10;       n /= 10;    }    cout

Finding ‘k’ such that its modulus with each array element is same in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:58:00

295 Views

In this tutorial, we are going to write a program that finds a number such that its modulus with each array element is same. Let's see an example.Input − arr = {10, 4, 2}Output − 1 2If there are two numbers x, y and x > y, assume that x - y = d.Then the x = y + d.Let's say we have a number k such that x%k = y%k. Apply modulo k for the above equation and find the value d.x%k = (y+d)%k y%k = y%k +d%k d%k = 0From the above calculation, if the number k is ... Read More

Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d) in C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:55:18

64 Views

In this tutorial, we are going to write a program that calculates ∆ X value that satisfies the given equation. The equation is (a + ∆ X)/(b + ∆ X) = c/d.Here, we need a little bit of math to solve the equation. And it's straightforward. Cross multiply and take ∆X to one side.You will get the value of ∆X as (b*c-a*d)/(d-c).We are given a, b, c, and d values. Finding \Delta XΔX value is straightforward.ExampleLet's see the code. Live Demo#include using namespace std; int findTheXValue(int a, int b, int c, int d) {    return (b * c - ... Read More

Advertisements