Found 33676 Articles for Programming

How to convert an integer to hexadecimal and vice versa in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:13:29

12K+ Views

Converting Integer to HexadecimalAn integer can be converted to a hexadecimal by using the string.ToString() extension method.Integer Value: 500 Hexadecimal Value: 1F4Converting Hexadecimal to Integer −A hexadecimal value can be converted to an integer using int.Parse or convert.ToInt32int.Parse − Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.Hexadecimal Value: 1F4 Integer Value: 500Convert.ToInt32 −Converts a specified value to a 32-bit signed integer.Hexadecimal Value: 1F4 Integer Value: 500Converting Integer to Hexadecimal −string hexValue = integerValue.ToString("X");Example Live Demousing System; namespace DemoApplication{    public class Program{       public static void ... Read More

How to validate an email address in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:08:58

6K+ Views

There are several ways to validate an email address in C#.System.Net.Mail −The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.System.Text.RegularExpressions − Represents an immutable regular expression.Use the below expression@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1, 3}\.[0-9]{1, 3}\.[0-9]{1, 3}\.)|(([a-zA-Z0-9\-]+\.)+))([azA-Z]{2, 4}|[0-9]{1, 3})(\]?)$"We can use the MailAddress class of the System.Net.Mail namespace to validate an email addressExample Live Demousing System; using System.Net.Mail; namespace DemoApplication{    class Program{       public static void Main(){          try{             string email = "hello@xyzcom";             Console.WriteLine($"The email is {email}"); ... Read More

Find paths from corner cell to middle cell in maze in C++

Arnab Chakraborty
Updated on 19-Aug-2020 11:09:16

364 Views

Suppose we have a square maze filled with numbers; we have to find all paths from a corner cell to the middle cell. Here, we will proceed exactly n steps from a cell in 4 directions Up, Down, Right and Left where n is the value of the cell. Thus, we can move to cell [i+n, j] to [i-n, j], [i, j+n], and [i, j-n] from a cell [i, j] where n is value of cell [i, j].So, if the input is like344473463675662662334325472655123656334301434334321335354326443351375363624345451then the output will be(0, 0)→(0, 3)→(0, 7)→(6, 7)→(6, 3)→(3, 3)→(3, 4)→(5, 4)→(5, 2)→(1, 2)→(1, 7)→(7, 7)→(7, 1)→(2, ... Read More

How to replace multiple spaces with a single space in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:06:06

4K+ Views

There are several ways to replace multiple spaces with single space in C#.String.Replace − Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.Replace(String, String, Boolean, CultureInfo)String.Join Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.Regex.Replace −In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.Example using Regex −Example Live Demousing System; using System.Text.RegularExpressions; namespace DemoApplication{    class Program{       ... Read More

Find pairs with given sum such that pair elements lie in different BSTs in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:03:13

136 Views

Suppose we have two given Binary Search Trees and another sum is given; we have to find pairs with respect of given sum so that each pair elements must be in different BSTs.So, if the input is like sum = 12then the output will be [(6, 6), (7, 5), (9, 3)]To solve this, we will follow these steps −Define a function solve() . This will take trav1, trav2, Sumleft := 0right := size of trav2 - 1res := a new listwhile left < size of trav1 and right >= 0, doif trav1[left] + trav2[right] is same as Sum, theninsert (trav1[left], ... Read More

How to return multiple values to caller method in c#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:02:11

1K+ Views

A Tuple can be used to return multiple values from a method in C#. It allows us to store a data set which contains multiple values that may or may not be related to each other. A latest Tuple called ValueTuple is C# 7.0 (.NET Framework 4.7).ValueTuples are both performant and referenceable by names the programmer chooses. ValueTuple provides a lightweight mechanism for returning multiple values from the existing methods. ValueTuples will be available under System.ValueTuple NuGet package.public (int, string, string) GetPerson() { }Example 1using System; namespace DemoApplication{    class Program{       public static void Main(){     ... Read More

Find pairs with given sum such that elements of pair are in different rows in Python

Arnab Chakraborty
Updated on 19-Aug-2020 11:00:13

185 Views

Suppose we have a matrix of unique elements and a sum; we have to find all the pairs from the matrix whose sum is equal to given sum. Here, each element of pair will be taken from different rows.So, if the input is like −24356987101114121311516sum = 13, then the output will be [(2, 11), (4, 9), (3, 10), (5, 8), (12, 1)]To solve this, we will follow these steps −res := a new listn := size of matrixfor i in range 0 to n, dosort the list matrix[i]for i in range 0 to n - 1, dofor j in range ... Read More

How to convert an integer to string with padding zero in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 10:59:55

2K+ Views

There are several ways to convert an integer to a string in C#.PadLeft − Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode characterToString − Returns a string that represents the current object.String Interpolation − The $ special character identifies a string literal as an interpolated string. This feature is available starting with C# 6.Example using string padding−Example Live Demousing System; namespace DemoApplication{    class Program{       public static void Main(){          int number = 5;         ... Read More

Query for ancestor-descendant relationship in a tree in C++

Ayush Gupta
Updated on 19-Aug-2020 10:56:20

165 Views

In this tutorial, we will be discussing a program to find query for ancestor-descendant relationship in a tree.For this we will be provided with a rooted tree and Q queries. Our task is to find the two roots given in the query is an ancestor of the other or not.Example Live Demo#include using namespace std; //using DFS to find the relation between //given nodes void performingDFS(vector g[], int u, int parent, int timeIn[], int timeOut[], int& count) {    timeIn[u] = count++;    for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i]; ... Read More

Find three integers less than or equal to N such that their LCM is maximum in C++

Ayush Gupta
Updated on 19-Aug-2020 10:54:33

115 Views

In this tutorial, we will be discussing a program to find three integers less than or equal to N such that their LCM is maximum.For this we will be provided with an integer value. Our task is to find other three integers smaller than the given value such that their LCM is maximum.Example Live Demo#include using namespace std; //finding three integers less than given value //having maximum LCM void findMaximumLCM(int n) {    if (n % 2 != 0) {       cout

Advertisements