C++ Articles

Page 36 of 597

Divisors of n-square that are not divisors of n in C++ Program

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 170 Views

In this tutorial, we are going to write a program that finds the divisors count of n-square and not n.It's a straightforward problem. Let's see the steps to solve the problem.Initialize the number n.Initialize a counter for divisors.Iterate from 2 to n^2n2.If the n^2n2 is divisible by the current number and nn is not divisible by the current number, then increment the count.Print the count.ExampleLet's see the code.#include using namespace std; int getNumberOfDivisors(int n) {    int n_square = n * n;    int divisors_count = 0;    for (int i = 2; i

Read More

Double Base Palindrome in C++ Program

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 414 Views

In this tutorial, we are going to write a program that checks whether the given number is a palindrome in two number systems.We have given a number and a base for another number system. We have to check whether the given number is a palindrome in the decimal number system and given number system.Let's see the steps to solve the problem.Initialize the number and number system base.Check whether the given number is a palindrome in the decimal number system or not.Convert the number into another number system in a string format.Check whether the converted number is palindrome or not.If the ...

Read More

Double the first element and move zero to end in C++ Program

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 189 Views

In this tutorial, we are going to write a program that doubles the first element and moves all the zeroes to the end of the given array.We have to double a number when there are tow the same elements in adjacent indices. After that, we have to add a zero to the array.Move all the zeroes in the array to the end.ExampleLet's see the code.#include using namespace std; void moveZeroesToEnd(int arr[], int n) {    int count = 0;    for (int i = 0; i < n; i++) {       if (arr[i] != 0) {   ...

Read More

Double Tree with examples in C++ Program

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 332 Views

In this tutorial, we are going to learn how to double the given tree.Let's see the steps to solve the problem.Create node class.Initialize the tree with dummy data.Write a recursive function to double the tree.Recursively traverse through the tree.Store the left node in a variable.After traversing add the data by creating a new node.Now, add the left node to the newly created node as a left child.Print the tree.ExampleLet's see the code.#include using namespace std; class node {    public:    int data;    node* left;    node* right; }; node* newNode(int data) {    node* Node = new ...

Read More

Create a new string by alternately combining the characters of two halves of the string in reverse in C++ Program

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 193 Views

In this tutorial, we are going to write a program that creates a new string by alternately combining the characters of the two halves of the string in reverse order.Let's see the steps to solve the problem.Initialize the string.Find the length of the string.Store the first half and second half string indexes.Iterate from the ending of the two halves of the string.Add each character to the new string.Print the new string.ExampleLet's see the code.#include using namespace std; void getANewString(string str) {    int str_length = str.length();    int first_half_index = str_length / 2, second_half_index = str_length;    string new_string ...

Read More

Count of different ways to express N as the sum of 1, 3 and 4 in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 885 Views

Given a positive number N as input. The goal is to find the number of ways in which we can express N as a sum of 1s, 3s and 4s only. For example, if N is 4 then it can be represented as 1+1+1+1, 3+1, 1+3, 4 so the number of ways will be 4.Let us understand with examples.For ExampleInput -  N=5Output - Count of different ways to express N as the sum of 1, 3 and 4 are: 6Explanation -  5 can be represented as:1+1+1+1+11+3+13+1+11+1+34+11+4Input - N=6Output - Count of different ways to express N as the sum of ...

Read More

Final string after performing given operations in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 262 Views

In this tutorial, we are going to solve the following problem.Given a string containing only characters a and b, our task is to delete the sub-string ab from the string. And print the remaining string.Here, the idea is very simple to solve the problem. Every string with only a's and b's will shrink to either a's or b's at the end.Let's see the steps to solve the problem.Initialize the string.Initialize two counter variables for a and b.Iterate over the given string.Count the a's and b'sFind the maximum from the a and b frequencies.Print the difference between the two.ExampleLet's see the ...

Read More

Find (a^b)%m where 'a' is very large in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 318 Views

In this tutorial, we are going to solve the equation (ab)%m where a is a very large number.The equation (ab)%m=(a%m)*(a%m)...b_times. We can solve the problem by finding the value of a%m and then multiplying it b times.Let's see the steps to solve the problem.Initialize the numbers a, b, and m.Write a function to find the a%m.Initialize the number with 0.Iterate over the number in string format.Add the last digits to the number.Update the number with number modulo them.Get the value of a%m.Write a loop that iterates b times.Multiply the a%m and modulo the result with m.Print the result.ExampleLet's see the ...

Read More

Find A and B from list of divisors in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 214 Views

In this tutorial, we are going to solve the below problem.Given an array of integers, we have to find two numbers A and B. All the remaining numbers in the array are the divisors of A and B.If a number is a divisor of both A and B, then it will present twice in the array.Let's see the steps to solve the problem.The max number in the array is one of the numbers from A and B. Let's say it is A.Now, B will be the second-largest number or the number which is not a divisor of A.ExampleLet's see the ...

Read More

Find 2^(2^A) % B in C++

Hafeezul Kareem
Hafeezul Kareem
Updated on 11-Mar-2026 177 Views

In this tutorial, we are going to write a program to evaluate the equation 2^(2^A) % B.We are going to find the value of the equation using a recursive function. Let's see the steps to solve the problem.Write a recursive function that takes 2 arguments A and B.If A is 1, then return 4 % B as 2^(2^1) % B = 4 % B.Else recursively call the function with A-1 and b.Return the result^2%B.Print the solutionExampleLet's see the code.#include using namespace std; long long solveTheEquation(long long A, long long B) {    // 2^(2^1) % B = 4 % ...

Read More
Showing 351–360 of 5,962 articles
« Prev 1 34 35 36 37 38 597 Next »
Advertisements