C++ Articles

Page 258 of 597

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 651 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 the factorial of a number in pl/sql using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 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)DECLARE    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

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ 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#include using namespace std; int countDigitInNum(long long number, int d) {    int count = 0;    while(number){       if((number % ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 242 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#include #include using namespace std; double factLog(int n) {    if (n

Read More

Toggle all characters in the string in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 1K+ Views

This program translates the characters of a string into uppercase. However, this task can be easily achieved by using the toUpper() method of the c++ class library. But in this program, we perform this by calculating the ASCII value of characters in uppercase. The Algorithm is as follows;AlgorithmSTART    Step-1: Declare the array of char    Step-2: Check ASCII value of uppercase characters which must grater than A and lesser than Z    Step-3: Check ASCII value of lower characters which must grater than A and lesser than Z ENDThe toggleChar() method gets the array of characters as an input. ...

Read More

Swap Upper diagonal with Lower in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 401 Views

This tutorial is designed to swap the upper row of a three-diagonal array to its lower one using c++ code. Moreover, if a three-diagonal array is an input, the coveted results must be something like that as;For this, the course of action is briefed in the algorithm as follows;AlgorithmStep-1: Input a diagonal array Step-2: Pass it to Swap() method Step-3: Traverse the outer loop till 3 Step-4: increment j= i+ 1 in the inner loop till 3 Step-5: put the array value in a temp variable Step-6: interchange the value arr[i][j]= arr[j][i] Step-7: put the temp data to arr[j][i] Step-8: ...

Read More

Replacing space with a hyphen in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 2K+ Views

In this C++ program, the space in the string will be replaced with the hyphen. Firstly, the length of the string is determined by the length() function of the cstring class, then hyphen is filled into the space of the sentence by traversing the string as follows.Example#include #include using namespace std; int main(){    // raw string declaration    string str = "Coding in C++ programming";    cout

Read More

Sqrt, sqrtl, and sqrtf in C++ programming

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 395 Views

Math ClassThis article demonstrates the usage of math class essentials functions sqrt(), sqrtl(), and sqrtf() to calculate the square root of double, long, and float type variables with precision respectively. The Math class of C++ offers a wide range of functions to calculate mathematical calculations including sin, cos, square root, ceil, floor, etc..It is, therefore, mandatory to import the definition of header class library in the program in order to avail all calculative methods.Sqrt MethodThe double sqrtl () method of the Math class returns the square root of a double variable with precision. The syntax of this function is ...

Read More

Validate IP Address in C++

Ajay yadav
Ajay yadav
Updated on 11-Mar-2026 3K+ Views

This article is serving the purpose of validating the correct IP (internet protocol) address by virtue of C++ code programming. The IP address is a 32-bit dot-decimal-notation, broken into four decimal numbers segments ranging from 0 to 255. Furthermore, these numbers are separated by dots consecutively. The IP address serves the purpose of identifying a host machine in the network in a unique manner in order to establish a connection among them.So, in order to validate the correct IP address input from the user-end, the following algorithm briefs how exactly the code sequence is materialized to identify the correct IP ...

Read More

Find closest number in array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ Views

Suppose we have an array A with n elements. And elements are sorted. We have to find the closest value to the given integer. The array may contain duplicate values and negative numbers. So if the array is like [2, 5, 6, 7, 8, 8, 9] and the target number is 4, then closest element is 5.We can solve this by traversing through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolute difference.Example#include #include using namespace std; int getNearest(int x, int y, int target) {   ...

Read More
Showing 2571–2580 of 5,962 articles
« Prev 1 256 257 258 259 260 597 Next »
Advertisements