Tutorialspoint

C# print one dimensional array in reverse order

using System; 
class Demo {     

   static void Main() {
	
      int[] arr = { 76, 12, 66, 90, 34, 2, 64 };
        
      // Initial Array
      Console.WriteLine("Original Array= ");
      foreach (int i in arr) {
         Console.WriteLine(i);
      }  
      // Reverse Array
      Array.Reverse(arr);
        
      Console.WriteLine("Reversed Array= ");
      foreach (int j in arr) {
         Console.WriteLine(j);
      }
      Console.ReadLine();
   }
}

C# read inputs as strings

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      string myStr;
        
      // use ReadLine() to read the entered line
      myStr = Console.ReadLine();
		
      // display the line
      Console.WriteLine("Result = {0}", myStr);
   }
}

C# read inputs as integers

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      string val;
       int res;
        
      Console.WriteLine("Input from user: ");
      val = Console.ReadLine();
        
      // convert to integer
      res = Convert.ToInt32(val);
		
      // display the line
      Console.WriteLine("Input = {0}", res);
   }
}

C# print a blank line

using System;
namespace Program {
   public class Demo {
      public static void Main(String[] args) {

         Console.WriteLine(" ");

         Console.WriteLine("Displayed a blank line above!\n");
         Console.ReadLine();

      }
   }
}

C# find date difference

using System;
namespace Program {
   class Demo {
      static int Main() {
         DateTime date1 = new DateTime(2018, 09, 15);
         Console.WriteLine("Date 1 : {0}", date1);
            
         DateTime date2 = new DateTime(2018, 09, 28);
         Console.WriteLine("Date 2 : {0}", date2);
         if (date1 < date2)
            Console.WriteLine("{0} comes before {1}", date1, date2);
         Console.Read();
         return 0;
 
      }
   }
}

C# substring from a string

using System;

public class Demo {
   public static void Main() {
      string s = "Tom Cruise";

      if (s.Contains("Tom") == true) {
	      Console.WriteLine("Substring found!");
      } else {
	      Console.WriteLine("Substring not found!");
      }
   }
}

C# find a number using Pythagoras Theorem

using System;
public class Program {
   public static void Main(string[] args) {
      double val1, val2, res;
      val1 = 10;
      val2 = 5;
      res = Math.Sqrt(val1 * val1 + val2 * val2);
        
      Console.WriteLine("Result = {0}", res);
      Console.ReadLine();
 
   }
}

C# remove an empty string from a list

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
   static void Main() {
      List<string> myList = new List<string>() {
         " ",
         " ",
         " "
      };
        
      Console.Write("Initial list with emoty strings...\n");
      foreach (string list in myList) {
         Console.WriteLine(list);
      }
        
      Console.Write("Removing  an empty element from the list...\n");
      myList.RemoveAt(0);

      foreach (string list in myList) {
         Console.WriteLine(list);
      }
      Console.WriteLine("Empty List after deleting an empty element is shown above...");
   }
}

C# remove an element from Array List

using System;
using System.Collections;
 
class Demo {
     
   static void Main() {
      ArrayList arr = new ArrayList();
      arr.Add( "One" );
      arr.Add( "Two" );
      arr.Add( "Three" );
      arr.Add( "Four" );
        
      Console.WriteLine("ArrayList...");
      foreach(string str in arr) {
         Console.WriteLine(str);
      }
        
      arr.Remove("Three");
        
      Console.WriteLine("ArrayList after removing an element...");

      foreach(string str in arr) {
         Console.WriteLine(str);
      }
        
      Console.ReadLine();
   }
}

C# print first ten Fibonacci numbers

using System;  
public class Demo {  
   public static void Main(string[] args) {  
      int val1 = 0, val2 = 1, val3, i, n;    
         
      n = 10;
         
      Console.WriteLine("1st ten Fibonacci numbers:");
      Console.Write(val1+" "+val2+" "); 
         
      for(i=2;i<n;++i) {    
         val3 = val1 + val2;    
         Console.Write(val3+" ");    
         val1 = val2;    
         val2 = val3;    
      }    
   }  
}

Previous 1 ... 3 4 5 6 7 8 9 ... 1624 Next
Advertisements
Loading...