Programming Articles - Page 2490 of 3363

abs() function for complex number in c++ ?

sudhir sharma
Updated on 07-Aug-2019 14:41:04

498 Views

The abs function in C++ is used to find the absolute value of a complex number. The absolute value of a complex number (also known as modulus) is the distance of that number from the origin in the complex plane. This can be found using the formula −For complex number a+bi:mod|a+bi| = √(a2+b2)The abs() function returns the result of the above calculation in C++. It is defined in the complex library that is needed to be included.PROGRAM TO SHOW USE OF abs() FUNCTION FOR COMPLEX NUMBERS IN C++#include #include using namespace std; int main () {    float ... Read More

A shorthand array notation in C for repeated values?

sudhir sharma
Updated on 07-Aug-2019 14:38:32

188 Views

An array stores number of same data type. For an array there may arise a situation when you need to store 2-3 values that are same i.e. 3,3,3,3 needs to be stored.For this case, the programing language C has made a simple way to create an array with such repeated value to reduce the workload of the programmer.Syntax[startofRepeatingSeq … EndofRepeatingSeq]number Example : For 3 repeated 5 times ; [0 … 4]3Example#include int main() {    int array[10] = {[0 ... 4]3, [6 ... 9]5};    for (int i = 0; i < 10; i++)       printf("%d ", array[i]);    return 0; }Output3 3 3 3 3 0 5 5 5 5

A modified game of Nim in C ?

sudhir sharma
Updated on 07-Aug-2019 14:32:16

715 Views

Modified game of Nim is an optimisation games of arrays. This game predicts the winner based on the starting player and optimal moves.Game Logic − In this game, we are given an array{}, that contains elements. There are generally two players that play the game namly player1 and player2. The aim of both is to make sure that all their numbers are removed from the array. Now, player1 has to remove all the numbers that are divisible by 3 and the player2 has to remove all the numbers that are divisible by 5. The aim is to make sure that ... Read More

A program to check if a binary tree is BST or not in C ?

sudhir sharma
Updated on 07-Aug-2019 14:32:47

431 Views

A binary tree is a tree data structure in which there are two child nodes for each node. The child nodes being two are referred as, left and right child.A BST is a tree structure in which left subtree contains nodes with values lesser than root and right subtree contains nodes with values greater that root.Here, we will check if a binary tree is a BST or not −To check for this we have to check for the BST condition on the binary tree. For a root node check for left child should be less that root, right child should ... Read More

How to remove the HTML tags from a given string in Java?

Vivek Verma
Updated on 28-Aug-2025 17:06:02

20K+ Views

A String is a final class in Java, and it is immutable, it means that we cannot change the object itself, but we can change the reference to the object. The HTML tags can be removed from a given string by using the following approaches: Using replaceAll() Method Using Pattern and Matcher Using replaceAll() Method We can remove the HTML tags from a given string by using the replaceAll() method. The replaceAll() method accepts two parameters: a "regular expression" and a "replacement string". It replaces all substrings that match the ... Read More

0-1 Knapsack Problem in C?

sudhir sharma
Updated on 07-Aug-2019 14:24:15

27K+ Views

A knapsack is a bag. And the knapsack problem deals with the putting items to the bag based on the value of the items. It aim is to maximise the value inside the bag. In 0-1 Knapsack you can either put the item or discard it, there is no concept of putting some part of item in the knapsack.Sample ProblemValue of items = {20, 25, 40} Weights of items = {25, 20, 30} Capacity of the bag = 50Weight distribution25, 20{1, 2} 20, 30 {2, 3} If we use {1, 3} the weight will be above the max allowed value. ... Read More

How to find If a given String contains only letters in Java?

Maruthi Krishna
Updated on 07-Aug-2019 12:13:04

2K+ Views

To verify whether a given String contains only characters −Read the String.Convert all the characters in the given String to lower case using the toLower() method.Convert it into a character array using the toCharArray() method of the String class.Find whether every character in the array is in between a and z, if not, return false.ExampleFollowing Java program accepts a String from the user and displays whether it is valid or not.import java.util.Scanner; public class StringValidation{    public boolean validtaeString(String str) {       str = str.toLowerCase();       char[] charArray = str.toCharArray();       for (int i ... Read More

While chaining, can we throw unchecked exception from a checked exception in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:32:08

794 Views

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is with out adjusting it as −try {    int result = (arr[a])/(arr[b]);    System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); }catch(ArithmeticException e) {    throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing higher ... Read More

How to check if an URL is valid or not using Java?

Maruthi Krishna
Updated on 09-Sep-2019 07:30:14

3K+ Views

The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource(file or, directory or a reference) in the worldwide web.This class provides various constructors one of them accepts a String parameter and constructs an object of the URL class. While passing URL to this method if you used an unknown protocol or haven’t specified any protocol this method throws a MalformedURLException.Similarly, the toURI() method of this class returns an URI object of the current URL. If the current URL is not properly formatted or, syntactically incorrect according to RFC 2396 this method throws ... Read More

Comparing Strings with (possible) null values in java?

Maruthi Krishna
Updated on 07-Aug-2019 11:59:25

6K+ Views

Strings in Java represents an array of characters. They are represented by the String class.Using compareTo() methodThe compareTo() method of the String class two Strings (char by char) it also accepts null values. This method returns an integer representing the result, if the value of the obtained integer is −0: Given two Strings are equal or, null.1 or less: The current String preceeds the argument.1 or more: The current String succeeds the argument.Exampleimport java.util.Scanner; public class CompringStrings {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your first string ... Read More

Advertisements