Found 34486 Articles for Programming

C# Program to get the difference between two dates in seconds

karthikeya Boyini
Updated on 23-Jun-2020 07:22:16

5K+ Views

Set two dates.DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 7, 15, 11, 14, 25);Now calculate the difference between two dates.TimeSpan ts = date2 - date1;Move further and calculate the difference in seconds.ts.TotalSecondsLet us see the complete code.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);       DateTime date2 = new DateTime(2018, 7, 15, 11, 14, 25);       TimeSpan ts = date2 - date1;       Console.WriteLine("No. of Seconds (Difference) = {0}", ts.TotalSeconds);    } }OutputNo. of Seconds (Difference) = 10745

C# Console.WindowHeight Property

Samual Sam
Updated on 23-Jun-2020 07:22:58

154 Views

The WindowHeight property gets or sets the height of the console window.Declare a variable.int height;Now, get the height of the current window.height = Console.WindowHeight;The following is the complete example −Example Live Demousing System; using System.Numerics; using System.Globalization; class Demo {    static void Main() {       int height;       height = Console.WindowHeight;       Console.WriteLine("Current window height = "+height);    } }OutputCurrent window height = 0

C# Program to add a node after the given node in a Linked List

Chandu yadav
Updated on 23-Jun-2020 07:24:57

210 Views

Set a LinkedList and add elements.string [] students = {"Beth", "Jennifer", "Amy", "Vera"}; LinkedList list = new LinkedList(students);Firstly, add a new node at the end.var newNode = list.AddLast("Emma");Now, use the AddAfter() method to add a node after the given node.list.AddAfter(newNode, "Matt");The following is the complete code.Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string [] students = {"Beth", "Jennifer", "Amy", "Vera"};       LinkedList list = new LinkedList(students);       foreach (var stu in list) {          Console.WriteLine(stu);       }       // ... Read More

C# Program to determine the difference in hours between two dates

Samual Sam
Updated on 23-Jun-2020 07:24:08

9K+ Views

Set two dates.DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);Now, get the difference between two dates.TimeSpan ts = date2 - date1;Get the result i.e. the difference in hours.ts.TotalHoursLet us see the complete code.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);       DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);       TimeSpan ts = date2 - date1;       Console.WriteLine("No. of Hours (Difference) = {0}", ts.TotalHours);    } }OutputNo. of Hours (Difference) = 794.984722222222

C# Percent ("P") Format Specifier

karthikeya Boyini
Updated on 23-Jun-2020 07:26:04

12K+ Views

The percent ("P") format specifier is used to multiply a number by 100.It converts the number into a string representing a % (percentage).We have the following double type −double val = .975746;If we will set (“P”) format specifier, then the above would result as −97.57 %In the same way, using (“P1”), would only include a single vale after decimal-point.97.6%Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double val = .975746;       Console.WriteLine(val.ToString("P", CultureInfo.InvariantCulture));       Console.WriteLine(val.ToString("P1", CultureInfo.InvariantCulture));       Console.WriteLine(val.ToString("P4", CultureInfo.InvariantCulture));    } }Output97.57 % 97.6 % 97.5746 %

C# Fixed-Point (“F”) Format Specifier

George John
Updated on 23-Jun-2020 07:25:28

751 Views

The ("F") format specifier converts a number to a string of the following form −"-ddd.ddd…"Above, "d" indicates a digit (0-9).Let us see an example.Here, if we will set (“F3”) format specifier to add three values after decimal places, for let’s say, 212.212.000The following is another example −Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       int val;       val = 38788;       Console.WriteLine(val.ToString("F",CultureInfo.InvariantCulture));       val = -344;       Console.WriteLine(val.ToString("F3",CultureInfo.InvariantCulture));       val = 5656;       Console.WriteLine(val.ToString("F5",CultureInfo.InvariantCulture));    } }Output38788.00 -344.000 5656.00000

C# Exponential (“E”) Format Specifier

karthikeya Boyini
Updated on 23-Jun-2020 07:15:36

869 Views

The ("E") format specifier converts a number to a string of the following form −"-d.ddd…E+ddd"Or"-d.ddd…e+ddd"Above, "d" is a digit (0-9).Prefix the exponent with an "E" or an "e".Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double d = 3452.7678;       Console.WriteLine(d.ToString("E", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("E10", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("e", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("e10", CultureInfo.InvariantCulture));    } }Output3.452768E+003 3.4527678000E+003 3.452768e+003 3.4527678000e+003

Implicit conversion from 64-bit signed integer (long) to Decimal in C#

Samual Sam
Updated on 23-Jun-2020 07:17:31

191 Views

The long type represents a 64-bit signed integer.To implicitly convert a 64-bit signed integer to a Decimal, firstly set a long value.long val = 989678876876876;To convert long to decimal, assign the value.dec = val;Let us see another example −Example Live Demousing System; public class Demo {    public static void Main() {       long val = 76755565656565;       decimal dec;       Console.WriteLine("Implicit conversion from 64-bit signed integer (long) to Decimal");       dec = val;       Console.WriteLine("Decimal : "+dec);    } }OutputImplicit conversion from 64-bit signed integer (long) to Decimal Decimal : 76755565656565

C# Program to skip elements from a sequence as long as the specified condition is true

Chandu yadav
Updated on 23-Jun-2020 07:17:02

172 Views

Use the SkipWhile() method to skip elements from a sequence as long as the specified condition is true.The following is the array −int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 };Here is the condition.s => s >= 50As long as the above condition is true, the elements above 50 are skipped as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 };       // skips elements above 50       IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >=          50);       // displays rest of the elements       Console.WriteLine("Skipped marks > 60...");       foreach (int res in selMarks) {          Console.WriteLine(res);       }    } }OutputSkipped marks > 60... 48 42 35

C# Program to skip elements of an array from the end

karthikeya Boyini
Updated on 23-Jun-2020 07:18:00

960 Views

Declare an array and initialize elements.int[] marks = { 45, 50, 60, 70, 85 };Use the SkipLast() method to skip elements of an array from the end.IEnumerable selMarks = marks.AsQueryable().SkipLast(3);The elements are skipped and rest of the elements is returned as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 45, 50, 60, 70, 85 };       Console.WriteLine("Array...");       foreach (int res in marks)       Console.WriteLine(res);       IEnumerable selMarks = marks.AsQueryable().SkipLast(3);       Console.WriteLine("Array after skipping last 3 elements...");       foreach (int res in selMarks)       Console.WriteLine(res);    } }OutputArray... 45 50 60 70 85 Array after skipping last 3 elements... 45 50

Advertisements