To check whether a given number is power of 2 in JavaScript, we can check if the number is generated using multiplying 2's only. We will be discussing 5 different approaches to check whether a given number is a power of 2. In this article we are having two numbers, our task is to check whether a given number is power of 2 in JavaScript. Users must be familiar with JavaScript Math functions, loops, binary representation and bitwise operators. Approaches to Check if a Number is Power of 2 Here is a list of approaches to check whether ... Read More
strdup() The function strdup() is used to duplicate a string. It returns a pointer to a null-terminated byte string. Syntax Here is the syntax of strdup() in C language, char *strdup(const char *string); Example Here is an example of strdup() in C language. #include #include int main() { char *str = "Helloworld"; char *result; result = strdup(str); printf("The string : %s", result); return 0; } Output The string : Helloworld strndup() The function strndup works similarly to the function strndup(). This function duplicates the string at most size bytes i.e. the given size in the function. It also returns ... Read More
In C++ the structure and class are basically the same. But there are some minor differences. These differences are like below. The class members are private by default, but members of structures are public. Let us see these two codes to see the differences. Example Code for Class Here is the following code for the class. #include using namespace std; class my_class { public: int x = 10; }; int main() { my_class my_ob; cout
Complex numbers are numbers that are expressed as a+bi, where i is an imaginary number and a and b, are real numbers. Some examples of complex numbers are − 2+3i 5+9i 4+2i Example A program to perform complex number multiplication is as follows − #include using namespace std; int main(){ int x1, y1, x2, y2, x3, y3; cout y1; cout y2; x3 = x1 * x2 - y1 * y2; y3 = x1 * y2 + y1 * x2; cout
In this tutorial, we are going to learn how to delete a node in the doubly linked list. Approach Let's see the steps to solve the problem. Write struct with data, prev, and next pointers. Write a function to insert the node into the doubly linked list. Initialize the doubly ... Read More
This is a simple way to read whole ASCII file into std::string in C++ − Algorithm Here is the algorithm we will follow to Read the whole ASCII file into C++ std::string. Begin Declare a file a.txt using file object f of ifstream type to perform a read operation. Declare a variable str of string type. If(f) Declare another variable ss of ostringstream type. Call rdbuf() function to read the data of the file object. Put the ... Read More
You can use the popen and pclose functions to pipe to and from processes. The popen() function opens a process by creating a pipe, forking, and invoking the shell. We can use a buffer to read the contents of stdout keep appending it to a result string and return this string when the processes exit. Example Here is the following example showing an execution of a command and getting the output of the command within C++ using POSIX. #include #include #include #include using namespace std; string exec(const string& command) { char buffer[128]; string result ... Read More
Centering content vertically in a full-screen div can be challenging if you are unfamiliar with Tailwind's utilities. Tailwind CSS has many utility classes, but to use them effectively, you must understand how they work and the best ways to apply them. Approaches for Vertically Aligning Content in a Div Tailwind CSS provides a wide range of utility classes that support effective alignment of the content within an element. We can vertically align div content in Tailwind CSS using the following approaches. Using Flexbox Using Grid Using ... Read More
Stream API was introduced by Java 8 for processing collections of data by a powerful and expressive way. One common question that arises when using streams is: What do I need to do to break out of, or return from, a forEach operation? In traditional loops you can break or return early. But the forEach method in streams doesn’t make this easy. This article examines why this is the case and examines alternate ways for early termination in the stream processing system.Read More: Java Stream API Improvements. Understanding Stream forEach ... Read More
Tracking differences between elements in an array is a fundamental task in data analysis and software development. Whether you're analyzing trends in numerical data, measuring changes over time, or debugging an application's behavior, calculating these differences can provide valuable insights. In JavaScript, this task is often approached using various techniques, including loops and modern functional programming methods like the map() function. Here are two methods to calculate differences between elements in an array: Iterative Loop and Functional Programming using the map. Problem Statement We are given an array of Number literals, and we are required to write a function that returns ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP