There are two time periods provided in the form of hours, minutes and seconds. Then their difference is calculated. For example: Time period 1 = 8:6:2 Time period 2 = 3:9:3 Time Difference is 4:56:59 C++ Program to Find Difference Between Two Times To find the time differences then check whether we need to borrow time, for example, adding 60 seconds if the second time has more seconds. Then just subtract the hours, minutes, and seconds of the second time from the first time to get the result. Example In this example, you will see the time difference of ... Read More
Pure functions always return the same result for the same argument values. They only return the result and there are no extra side effects like argument modification, I/O stream, output generation etc. Following are the examples of pure vs impure functions: Pure Function: sin(), strlen(), sqrt(), max(), pow(), floor() etc. Impure Function: rand(), time(), etc. There are many pure functions in C language such as strlen(), strcmp(), strchr(), strrchr(), strstr(), toupper(), tolower(), abs(), sin(), cos(), sqrt(), exp(), log(), pow(), fabs(), etc. Here we are explaining a few pure functions with ... Read More
To print random numbers within a range, we will have two input variables where we need to set the maximum and minimum values. Using these two values, we can display a random value within the specified range. Example Following is the input-output statement to understand the range of a random number: Input: min = 4, max = 14 Output: 8, 10, 7 Explanation: Any numeric value between 4 and 14 can be displayed in the specified range. Generate Random Number Within a Range Using rand() with Modulus The rand() function generates the random number while modulus operator return ... Read More
Python uses the try-except block to handle errors during runtime. But as your program grows, handling exceptions can get messy with repeated code or too many nested blocks. Using cleaner methods for exception handling can reduce duplication, and make your code easier to manage. In this article, we explore better ways to handle exceptions in Python. Using Specific Exception Types It is best to catch specific exceptions like ValueError or FileNotFoundError instead of using a general except: block. This prevents hiding unexpected errors and improves debugging This approach avoids catching unrelated exceptions like KeyboardInterrupt or TypeError. Example In this ... Read More
Custom exceptions in Python allow you to create meaningful error types that fit your application's needs. By adding error codes and error messages to custom exceptions, you can provide structured (organized) information when errors occur. Although Python has many built-in exceptions, custom exceptions are well-suited to explain specific problems in your program. Including error codes and messages, handling errors, and debugging your code. Creating a Custom Exception with Error Code You can create a custom exception by defining a class that inherits from Python's built-in Exception class. This allows you to include additional details, such as a custom error message ... Read More
In C++, a global variable is a variable that is declared outside of any function or class. It can be accessible from any part of the function. Declaration of a Global Variable The global variables are declared after the heading file inclusions section and before starting the main() function. The declaration is simple just like a normal variable declaration. You need to use data type and variable name. You can also initialize them if required. Syntax The following is the basic syntax of declaring global variable: datatype global_var_name = var_value; Example of Global Variable Declaration In this example, we ... Read More
The queue which is implemented as FIFO where insertions are done at one end (rear) and deletions are done from another end (front). The first element that entered is deleted first. Queue operations EnQueue: Insertion at rear end. DeQueue(): Deletion from front end. But a priority queue doesn’t follow First-In-First-Out, but rather than each element has a priority based on the level of importance. Items with the same priority are processed on First-In-First-Out service basis. An item with higher priority ... Read More
To check if a directed graph is connected or not, we need to check if there exists a path between every pair of vertices. A directed graph (or digraph) is a graph where each edge has a direction, edges are in ordered pairs, and edges traverse from the source vertex (the tail) to the destination vertex (the head). In this article, we have a directed graph with five vertices and its respective adjacency matrix. Our task is to use Depth-first Search(DFS) to check the connectivity of the given graph. Example of Connected Graph In the figure given below, we have ... Read More
To check a strongly connected graph using Kosaraju's algorithm, we need to understand the strongly connected graph and Kosaraju's algorithm. For a graph to be strongly connected, it should be a directed graph, and for any pair of vertices u and v in the directed graph, there exists a directed path from u to v and a directed path from v to u. In this article, we have a directed graph with five vertices. Our task is to use Kosaraju's algorithm to check if the given graph is strongly ... Read More
In this article, we'll learn how to replace a character at a specific index in a string. Strings in Java are sequences of characters enclosed in double-quotes (" "). We use strings to represent text in our programs. Problem Statement You are given a string, an index, and a character. Your task is to replace the character at the specified index in the string with the new character. Input string: Java Programming, Index: 6 Output: Java P%ogramming Different Approaches There are multiple ways to achieve this in Java. Below are two common approaches: Using substring() ... Read More