Hi Friends, hope you are doing good. This time we are going to share a collection of basic c# simple programs for the interview. Just take a look at this tutorial before going for interviews.
Program 1: Add Two Numbers Using The C sharp Solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | static void Main(string[] args) { Console.WriteLine("Addition of two Numbers"); Console.WriteLine("***********************"); Console.WriteLine("Enter First Number"); int first = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Second Number"); int second = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Addition of " + first + " + " + second + " = " + Convert.ToString(first + second)); Console.ReadLine(); } |
Program 2: Prime Number Program in C# Solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using System; public class PrimeNumberProgram { public static void Main(string[] args) { int n, i, m=0, flag=0; Console.Write("Enter any number to check prime number: "); n = int.Parse(Console.ReadLine()); m=n/2; for(i = 2; i <= m; i++) { if(n % i == 0) { Console.Write("This is not Prime Number."); flag=1; break; } } if (flag==0) Console.Write("This is a prime number."); } } |
Program 3: C# Program to Find Fibonacci Series Solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using System; public class FibonacciExample { public static void Main(string[] args) { int n1=0,n2=1,n3,i,number; Console.Write("Enter the number of elements: "); number = int.Parse(Console.ReadLine()); Console.Write(n1+" "+n2+" "); for(i=2;i<number;++i) { n3=n1+n2; Console.Write(n3+" "); n1=n2; n2=n3; } } } |
Program 4: C# program to compare two dates Solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using System; namespace DateAndTime { class Program { static int Main() { DateTime sd = new DateTime(2018, 03, 16); Console.WriteLine("Starting Date : {0}", sd); DateTime ed = sd.AddDays(10); Console.WriteLine("Ending Date : {0}", ed); if (sd < ed) Console.WriteLine("{0} Occurs Before {1}", sd, ed); Console.Read(); return 0; } } } |
Program 5: C# program to implement phonebook Solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | using System; using System.Collections; using System.IO; class PhoneBook { static void Main(string[] arg) { Hashtable tab = new Hashtable(); string fileName; if { (arg.Length > 0) fileName = arg[0]; } else { fileName = "phoneBook.txt"; } StreamReader r = File.OpenText(fileName); string line = r.ReadLine(); while (line != null) { int pos = line.IndexOf('='); string name = line.Substring(0, pos).Trim(); long phone = Convert.ToInt64(line.Substring(pos + 1)); tab[name] = phone; line = r.ReadLine(); } r.Close(); for (; ; ) { Console.Write("Name : "); string name = Console.ReadLine().Trim(); if (name == "") break; object phone = tab[name]; if (phone == null) Console.WriteLine("-- Not Found in Phone Book"); else Console.WriteLine(phone); } } } |
Program 6: Palindrome Number Program in C# Solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using System; public class PalindromeNumberProgram { public static void Main(string[] args) { int n,r,sum=0,temp; Console.Write("Enter any number to check palindrome number: "); n = int.Parse(Console.ReadLine()); temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum) { Console.Write("This number is Palindrome."); } else { Console.Write("This number is not Palindrome"); } } } |
Program 7: Multiply Two Numbers Using C# Solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | static void Main(string[] args) { Console.WriteLine("Multiplication of two Numbers"); Console.WriteLine("*****************************"); Console.WriteLine("Enter First Number"); int first = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Second Number"); int second = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Multiplication of " + first + " x " + second + " = " + Convert.ToString(first * second)); Console.ReadLine(); } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.