Programming Articles - Page 2283 of 3366

C++ program to implement Collatz Conjecture

Ayush Gupta
Updated on 03-Dec-2019 10:23:59

447 Views

In this tutorial, we will be discussing a program to implement Collatz Conjecture.For this, we will be given with a number n and we have to find out whether it can be converted to 1 using two operations −If n is even, n is converted to n/2.If n is odd, n is converted to 3*n + 1.Example#include using namespace std; //checking if n reaches to 1 or not bool check1(int n, unordered_set &s){    if (n == 1)       return true;    if (s.find(n) != s.end())       return false;    return (n % 2)? check1(3*n + ... Read More

Array.BinarySearch(Array, Object) Method with examples in C#

AmitDiwan
Updated on 03-Dec-2019 10:27:25

180 Views

The Array.BinarySearch(Array, Object) method in C# is used to search an entire one-dimensional sorted array for a specific element, using the IComparable interface implemented by each element of the array and by the specified object.Syntaxpublic static int BinarySearch (Array arr, object val);Above, arr is the sorted 1-D array, whereas val is the object to search for.Example Live Demousing System; public class Demo {    public static void Main() {       int[] intArr = {5, 10, 15, 20};       Array.Sort(intArr);       Console.WriteLine("Array elements...");       foreach(int i in intArr) {          Console.WriteLine(i); ... Read More

C++ program to get the Sum of series: 1 – x^2/2! + x^4/4! -…. upto nth term

Ayush Gupta
Updated on 03-Dec-2019 10:20:32

1K+ Views

In this tutorial, we will be discussing a program to get the sum of series 1 – x^2/2! + x^4/4! … upto nth term.For this we will be given with the values of x and n. Our task will be to calculate the sum of the given series upto the given n terms. This can be easily done by computing the factorial and using the standard power function to calculate powers.Example#include #include //calculating the sum of series double calc_sum(double x, int n){    double sum = 1, term = 1, fct, j, y = 2, m;    int ... Read More

C# Copy() Method

AmitDiwan
Updated on 03-Dec-2019 10:19:27

495 Views

The Copy() method in C# is used to create a new instance of String with the same value as a specified String.Syntaxpublic static string Copy (string s);Above, s is the string to copy.Example Live Demousing System; public class Demo {    public static void Main(string[] args) {       string s1 = "Amy";       string s2 = "Katie";       string s3 = String.Copy(s2);       Console.WriteLine("String1 = "+s1);       Console.WriteLine("String2 = "+s2);       Console.WriteLine("Are both the strings equal? = "+s1.Equals(s2));       Console.WriteLine("Are both the strings equal? = "+s2.Equals(s3));   ... Read More

C++ program to generate random alphabets

Ayush Gupta
Updated on 03-Dec-2019 10:17:22

906 Views

In this tutorial, we will be discussing a program to generate random alphabets.For this, we will have a fixed size of array/string and use the rand() function to generate a random string of alphabets.Example#include using namespace std; const int MAX = 26; //generating a string of random alphabets string gen_random(int n){    char alphabet[MAX] = {       'a', 'b', 'c', 'd', 'e', 'f', 'g',       'h', 'i', 'j', 'k', 'l', 'm', 'n',       'o', 'p', 'q', 'r', 's', 't', 'u',       'v', 'w', 'x', 'y', 'z'    };    string res ... Read More

C++ program to generate CAPTCHA and verify user

Ayush Gupta
Updated on 03-Dec-2019 10:13:06

829 Views

In this tutorial, we will be discussing a program to generate CAPTCHA and verify user.For this, we will provide the user with a random string and ask him to reenter the same string. Then it has to be checked if the given and the input string matches.The CAPTCHA should be completely random system generated consisting of a-z, AZ and 0-9.Example#include using namespace std; //checks if the strings are same bool check_string(string &captcha, string &user_captcha){    return captcha.compare(user_captcha) == 0; } //generates a random string as Captcha string gen_captcha(int n){    time_t t;    srand((unsigned)time(&t));    char *chrs = "abcdefghijklmnopqrstuvwxyzABCDEFGHI" "JKLMNOPQRSTUVWXYZ0123456789"; ... Read More

StringBuilder.ToString() Method in C#

AmitDiwan
Updated on 03-Dec-2019 09:57:10

2K+ Views

The StringBuilder.ToString() method in C# is used to convert the value of a StringBuilder to a String.SyntaxThe syntax is as follows −public override string ToString (); public string ToString (int begnIndex, int len);Above, the parameter begnIndex is the starting position of the substring in this instance, whereas len is the length of the substring.ExampleLet us now see an example − Live Demousing System; using System.Text; public class Demo{    public static void Main(){       StringBuilder strBuilder = new StringBuilder("Katie");       Console.WriteLine("String = "+strBuilder.ToString());       Console.WriteLine("StringBuilder capacity = "+strBuilder.Capacity);       Console.WriteLine("StringBuilder length = "+strBuilder.Length); ... Read More

Random.Next() Method in C#

AmitDiwan
Updated on 03-Dec-2019 09:54:27

3K+ Views

The Random.Next() method in C# is used to return a non-negative random integer.SyntaxThe syntax is as follows −public virtual int Next (); public virtual int Next (int maxVal);Above, the maxVal parameter is the exclusive upper bound of the random number to be generated.ExampleLet us now see an example − Live Demousing System; public class Demo {    public static void Main(){       Random r = new Random();       Console.WriteLine("Random numbers.....");       for (int i = 1; i

SortedDictionary.Values Property in C#

AmitDiwan
Updated on 03-Dec-2019 09:52:19

181 Views

The SortedDictionary.Value property in C# is used to get a collection containing the values in the SortedDictionary.SyntaxThe syntax is as follows −public System.Collections.Generic.SortedDictionary.ValueCollection Values { get; }ExampleLet us now see an example − Live Demousing System; using System.Collections; using System.Collections.Generic; public class Demo {    public static void Main(){       SortedDictionarysortedDict = new SortedDictionary();       sortedDict.Add(1, "Ultrabook");       sortedDict.Add(2, "Alienware");       sortedDict.Add(3, "Notebook");       sortedDict.Add(4, "Connector");       sortedDict.Add(5, "Flash Drive");       sortedDict.Add(6, "SSD");       sortedDict.Add(7, "HDD");       sortedDict.Add(8, "Earphone");       Console.WriteLine("SortedDictionary ... Read More

Double.IsNegativeInfinity() Method in C#

AmitDiwan
Updated on 03-Dec-2019 09:48:11

69 Views

The Double.IsNegativeInfinity() method in C# is used to return a value indicating whether the specified number evaluates to negative infinity.SyntaxThe syntax is as follows −public static bool IsNegativeInfinity (double val);Above, val is a double-precision floating-point number.ExampleLet us now see an example − Live Demousing System; public class Demo {    public static void Main(){       double d = 0.0/0;       Console.WriteLine("Double Value = "+d);       Console.WriteLine("HashCode of Double Value = "+d.GetHashCode());       TypeCode type = d.GetTypeCode();       Console.WriteLine("TypeCode of Double Value = "+type);       Console.WriteLine("Positive Infinity? = "+Double.IsInfinity(d));   ... Read More

Advertisements