Csharp Articles

Page 86 of 196

How to print a diamond using nested loop using C#?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 448 Views

With C#, you can easily display the following diamond shape.$ $$$ $$$$$ $$$$$$$ $$$$$$$$$ $$$$$$$ $$$$$ $$$ $To display a diamond shape, you need to focus on the following points −Number of rows Dollar sign to be displayed Empty spacesConsidering the above you can easily create the diamond shape as shown in the below code −Exampleusing System; namespace Program {    public class Demo {       public static void Main(String[] args) {          int i, j, r, d, e;          // rows = 5          r = 5;          // display dollar sign          d = 1;          // empty space          e = r - 1;          for (i = 1; i < r * 2; i++) {             // display empty space             for (j = 1; j

Read More

How to print a line on the console using C#?

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 800 Views

To display a line, Console.Write() is used in C#.Console displays the result on the console. I have first set a string.string str = "Tom Hanks is an actor";Now displaying the above line.Console.WriteLine(str);The following is the complete code −Exampleusing System; namespace Program {    public class Demo {       public static void Main(String[] args) {          string str = "Tom Hanks is an actor";          Console.WriteLine("Displaying a line below");          Console.WriteLine(str);          Console.ReadLine();       }    } }OutputDisplaying a line below Tom Hanks is an actor

Read More

C# program to get max occurred character in a String

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 917 Views

To get the maximum occurred character in a string, loop until the length of the given string and find the occurrence.With that, set a new array to calculate −for (int i = 0; i < s.Length; i++)    a[s[i]]++; }The values we used above −String s = "livelife!"; int[] a = new int[maxCHARS];Now display the character and the occurrence −for (int i = 0; i < maxCHARS; i++)    if (a[i] > 1) {       Console.WriteLine("Character " + (char) i);       Console.WriteLine("Occurrence = " + a[i] + " times");    }Let us see the complete code ...

Read More

Formatted output in C#

George John
George John
Updated on 11-Mar-2026 2K+ Views

To format output in C#, let us see examples to format date and double type.Set formatted output for Double type.Exampleusing System; class Demo {    public static void Main(String[] args) {       Console.WriteLine("Three decimal places...");       Console.WriteLine(String.Format("{0:0.000}", 987.383));       Console.WriteLine(String.Format("{0:0.000}", 987.38));       Console.WriteLine(String.Format("{0:0.000}", 987.7899));       Console.WriteLine("Thousands Separator...");       Console.WriteLine(String.Format("{0:0, 0.0}", 54567.46));       Console.WriteLine(String.Format("{0:0, 0}", 54567.46));    } }OutputThree decimal places... 987.383 987.380 987.790 Thousands Separator... 54, 567.5 54, 567Set formatted output for DateTimeExampleusing System; static class Demo {    static void Main() { ...

Read More

C# program to print duplicates from a list of integers

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

To print duplicates from a list of integers, use the ContainsKey.Below, we have first set the integers.int[] arr = {    3,    6,    3,    8,    9,    2,    2 };Then Dictionary collection is used to get the count of duplicate integers.Let us see the code to get duplicate integers.Exampleusing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          int[] arr = {             3,             6,       ...

Read More

C# program to print all distinct elements of a given integer array in C#

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 636 Views

We have set an array and a dictionary to get the distinct elements.int[] arr = {    88,    23,    56,    96,    43 }; var d = new Dictionary < int, int > ();Dictionary collection allows us to get the key and value of a list.The following is the code to display distinct elements of a given integer array −Exampleusing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          int[] arr = {             88,     ...

Read More

C# Program to perform Currency Conversion

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 5K+ Views

Let’s say you need to get the value of 10 dollars in INR.Firstly, set the variables: double usd, inr, val;Now set the dollars and convert it to INR.// how many dpllars usd = 10; // current value of US$ val = 69; inr = usd * val;Let us see the complete code −Exampleusing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          Double usd, inr, val;          // how many dpllars          usd = 10;          // current value of US$          val = 69;          inr = usd * val;          Console.WriteLine("{0} Dollar = {1} INR", usd, inr);       }    } }Output10 Dollar = 690 INR

Read More

C# program to split the Even and Odd integers into different arrays

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

Take two arrays:int[] arr2 = new int[5]; int[] arr3 = new int[5];Now, if the array element gets the remainder 0 on dividing by 2, it is even. Get those elements and add in another array. This loops through the length of the array:if (arr1[i] % 2 == 0) {    arr2[j] = arr1[i]; }In the else condition, you will get the odd elements. Add them to a separate array and display them individually as shown in the below example:Exampleusing System; namespace Demo {    public class Program {       public static void Main(string[] args) {       ...

Read More

C# program to convert time from 12 hour to 24 hour format

George John
George John
Updated on 11-Mar-2026 12K+ Views

Firstly, set the 12 hr format date.DateTime d = DateTime.Parse("05:00 PM");Now let us convert it into 24-hr format.d.ToString("HH:mm"));The following is the code to covert time from 12 hour to 24 hour format −Exampleusing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          DateTime d = DateTime.Parse("05:00 PM");          Console.WriteLine(d.ToString("HH:mm"));       }    } }Output17:00

Read More

C# program to find Largest, Smallest, Second Largest, Second Smallest in a List

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

Set the listvar val = new int[] {    99,    35,    26,    87 };Now get the largest number.val.Max(z => z);Smallest numberval.Min(z => z);Second largest numberval.OrderByDescending(z => z).Skip(1).First();Second smallest numberval.OrderBy(z => z).Skip(1).First();The following is the code −Exampleusing System; using System.Linq; public class Program {    public static void Main() {       var val = new int[] {          99,          35,          26,          87       };       var maxNum = val.Max(z => z);       Console.WriteLine("Maximum ...

Read More
Showing 851–860 of 1,951 articles
« Prev 1 84 85 86 87 88 196 Next »
Advertisements