A matrix is invertible if it has an inverse. An inverse of a matrix exists only when its determinant is non-zero. If the determinant of a matrix is zero, the matrix is called singular and cannot be inverted. In this article, we'll write a C++ program to check if a matrix is invertible using its determinant. To better understand this, let's take the following 3x3 matrix as an example: Given 3*3 Matrix: 4 2 1 2 1 1 9 3 2 To check if this matrix is invertible, we ... Read More
The LU decomposition of a matrix produces a matrix as a product of its lower triangular matrix and upper triangular matrix. In the lower triangular matrix (L), all values above the main diagonal are zero, and in the upper triangular matrix (U), all values below the main diagonal are zero. In this article, we'll write a C++ program to perform LU decomposition of any matrix. Example Steps to perform LU Decomposition of Matrix LU Decomposition breaks a matrix A into two matrices L (lower triangular) and U (upper triangular) such that: A = L * U We ... Read More
Shuffling an array means rearranging its elements in a random order. The Fisher-Yates algorithm creates a shuffle where every possible order is equally likely to happen. In this article, we'll show you how to write a C++ program that uses Fisher-Yates algorithm to shuffle an array. Fisher-Yates Algorithm to Shuffle an Array To shuffle an array, we use the Fisher-Yates algorithm. It works by swapping each element with another randomly selected element from the part of the array that we haven't shuffled yet. Here are the steps we follow: First, we start ... Read More
In C and C++, every character like 'A', 'b', '3', or '@' is stored as a number called its ASCII value. For example, 'A' is 65, and 'a' is 97. Given an integer like 97, we can convert it to its corresponding ASCII character which is 'a'. In this article, we will learn how to write a C and C++ program to convert an integer into its ASCII character. For example, we're given any integer from 0 to 127 (because the ASCII table contains 128 characters), and we need to convert it into its corresponding ASCII character: Input: 65 ... Read More
Here we will see how to check how many digits are there in an integer in C++. At first we will see the traditional rule, then a shorter method and finally another approach to find the digit count. For example, given a single integer (which can be positive, negative, or zero): //Example 1 Input: 12345 Output: 5 //Example 2 Input: -789 Output: 3 Note: The minus sign (if present) is not counted as a digit. Determining how many digits there are in an integerWe can count the number of digits in an integer using different methods ... Read More
Java Program to Add Characters to a String Java provides different ways to add characters to a string. We can add characters in the beginning or at the end or at any position according to the need. In this article, we are going to see three different approaches to do so: Using '+' operator Using methods of StringBuffer and StringBuilder classes ... Read More
In C language, file handling is used for various file actions such as to write, read, merge, etc. Merging Contents of Two Files into a Third FileTo merge the contents of two files into a third file, you need to open the first two files (whose content will be merged into the third file) in read mode and the third file in write mode. After opening the files, read the contents of the first file and write them to the third file, and similarly with the second file and appending them to the third file. Example Scenario Imagine these are ... Read More
The nested class that is defined inside the body of a method or within a loop, like for and if, is called a local inner class. Before making a Java program to check whether a given class is a local inner class or not, we need to understand the concept of nested classes first. Let's discuss this in detail. Nested Class When we create a class within another class, it is referred to as a nested class. The nested classes share the same scope. It allows the grouping of similar classes and encapsulation (wrapping of similar functionalities in a single unit). ... Read More
In C++, a conversion constructor is a special type of constructor that takes only one argument. It enables automatic type conversion from the argument's type to the class type. When an object of the class to be created from a single value(int). then the compiler will call the conversion constructor to create the object from that value. This can be implicit conversion of a value into a class object. Creating Conversion Constructor Following is the syntax to the Conversion Constructor in C++: class ClassName { public: ClassName(Type arg); // Conversion constructor }; Here, ... Read More
The character constant is a constant that is enclosed in a single quote (' '). It represents the integer value (i.e., ASCII) of the character. In C programming language, the data type of character constant is an integer (represented by int) and it needs 4 bytes to store a character constant. For example: 'A', '1', '3', etc., are the character constants. In C++, the data type for character constants is char and it needs only 1 byte to store a character constant. Here are the size of different data types in bytes: ... Read More