Articles on Trending Technologies

Technical articles with clear explanations and examples

Mersenne Prime Number in C++.

Narendra Kumar
Narendra Kumar
Updated on 31-Oct-2019 769 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 unit place digit of sum of N factorials using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 255 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 number of pairs in an array such that their XOR is 0 using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 237 Views

Suppose we have an array of n elements; we have to find a number of pairs in the array whose XOR will be 0. The pair (x, y) whose XOR is 0, then x = y. To solve it we can sort the array, then if two consecutive elements are the same, increase the count. If all elements are the same, then the last count may not be counted. In that case, we will check whether the last and first elements are the same or not, if the same, then increase the count by 1.Example#include #include using namespace std; int countPairs(int arr[],  int n) {     int count = 0; ...

Read More

Find max in struct array using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 790 Views

Here we will see how to get max in the struct array. Suppose there is a struct like below is given. We have to find the max element of an array of that struct type. struct Height{     int feet, inch; }; The idea is straight forward. We will traverse the array, and keep track of the max value of array element in inches. Where value is 12*feet + inch Example #include #include using namespace std; struct Height{     int feet, inch; }; int maxHeight(Height h_arr[], int n){     int index = 0;     int height = INT_MIN;     for(int i ...

Read More

Find last two digits of sum of N factorials using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 257 Views

Here we will see how to get the last two digits. The unit place digit and the tens place digit of the sum of N factorials. So if N = 4, it will be 1! + 2! + 3! + 4! = 33. so unit place is 3 and ten place is 3. The result will be 33.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. And after N > 10, the ten places will remain 0. For N ...

Read More

Find floor and ceil in an unsorted array using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Oct-2019 747 Views

Here we will see how to find the floor and ceiling in an unsorted array. The floor value is larger element which is smaller than or equal to x, and the ceiling value is smallest value which is larger than x. If the array A = [5, 6, 8, 9, 6, 5, 5, 6], and x is 7, then the floor value is 6, and the ceiling value is 8.To solve this problem, we will follow the linear search approach. We will traverse the array and track two distances with respect to x.Min distance of element greater than or equal ...

Read More

Find column with maximum sum in a Matrix using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Oct-2019 540 Views

Suppose we have a matrix of size M x N. We have to find the column, that has a maximum sum. In this program we will not follow some tricky approach, we will traverse the array column-wise, then get the sum of each column, if the sum is the max, then print the sum and the column index.Example#include #define M 5 #define N 5 using namespace std; int colSum(int colIndex, int mat[M][N]){    int sum = 0;    for(int i = 0; i maxSum) {           maxSum = sum;           index = i;       }    }    cout

Read More

Find an equal point in a string of brackets using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Oct-2019 498 Views

Here we will see how to get the equal points in a string of brackets. The equal point is the index I, such that the number of opening brackets before it is equal to the number of the closing bracket after it. Suppose a bracket string is like "(()))(()()()))))", if we see closer, we can getSo the number of opening brackets from 0 to 9 is 5, and the number of the closing brackets from 9 to 14 is also 5, so this is the equal point.To solve this problem, we have to follow these few steps −Store the number ...

Read More

How to determine if network type (2G, 3G or 4G) in Android?

Azhar
Azhar
Updated on 25-Oct-2019 949 Views

This example demonstrates how do I determine if network type (2G, 3G, 4G) in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.javaimport androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.telephony.TelephonyManager; import android.widget.Toast; import java.util.Objects; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       getNetworkClass(getApplicationContext());    }    public ...

Read More

Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 24-Oct-2019 183 Views

Here we will see how to generate one symmetric matrix of order N, and the elements of each row will contain numbers from 0 to N – 1. The diagonal elements will be 0 always. This task is easy, we will form a matrix of N x N, then for each row i and for each column j, if i and j are same, then mark it as 0, otherwise increase one counter from 1 to N – 1, place the values for each individual row. Example #include using namespace std; void makeSymmetricMatrix(int n) {     int matrix[n][n];     for(int i = 0; i

Read More
Showing 56751–56760 of 61,297 articles
Advertisements