Pages

Follow us on Facebook

C# Mostly asked basic interview programes.....click to view all.


---------------------------------------------------------------------------------
Program to calculate factorial of a number:

namespace FactorialNo
{
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine("Enter a number");

            int number = int.Parse(Console.ReadLine());

            int fact = 1;


            while (number > 0)
            {
                fact += fact * number;
                number--;
            }
            Console.WriteLine(fact);
            Console.ReadKey();
        }
    }
}

---------------------------------------------------------------------------------



Program to reverse a string:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a string");
            string str = Console.ReadLine();
            string revstr = null;
            int length = str.Length - 1;

            while(length>=0)
            {
                revstr = revstr + str[length];
                length--;
            }

            Console.WriteLine(revstr);
            Console.ReadKey();
        }
    }
}

---------------------------------------------------------------------------------
 


Program to check whether a string is palindrome or not:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a string");
            string str = Console.ReadLine();
            string revstr = null;
            int length = str.Length - 1;

            while(length>=0)
            {
                revstr = revstr + str[length];
                length--;
            }

             if(str==revstr)
            {
                Console.WriteLine("The provided string is palindrome.");
            }

            else
            {
                Console.WriteLine("The provided string is not palindrome.");
            }
            Console.WriteLine(revstr);
            Console.ReadKey();
        }
    }
}
 

---------------------------------------------------------------------------------

Program to reverse a number:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number");
            int number = int.Parse(Console.ReadLine());
            int revno = 0;

            while(number>0)
            {
                int rem = number % 10;

                revno = (revno * 10) + rem;
                number = number / 10;
            }

            Console.WriteLine(revno);
            Console.ReadKey();
        }
    }
}


---------------------------------------------------------------------------------


Program to calculate Fibonacci series:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0, count = 0, f1 = 0, f2 = 1, f3 = 0;

            Console.WriteLine("Enter a number for calcualting factorial.");

            count = int.Parse(Console.ReadLine());

            Console.Write(f1);

            Console.Write(" "+f2);

            for (i = 0; i < count-2; i++)
            {
               
                f3 = f1 + f2;

                Console.Write(" "+f3);

                f1 = f2;
                f2 = f3;

            }

            Console.ReadKey();
        }
    }
}


---------------------------------------------------------------------------------




Program to calculate the sum of int values in C#  using array:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] int_arr = new int[] { 1, 2, 3, 4, 5 };

            int sum = 0;

            for (int i = 0; i < int_arr.Length; i++)
            {
                sum += int_arr[i];
            }
            Console.WriteLine("Sum = "+sum);

            Console.ReadKey();
        }
    }
}

---------------------------------------------------------------------------------


Program to calculate average of int values using array:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] int_arr = new int[] { 1, 2, 3, 4, 5 };

            int sum = 0;

            for (int i = 0; i < int_arr.Length; i++)
            {
                sum += int_arr[i];
            }

            double average = sum / int_arr.Length;

            Console.WriteLine("Sum = "+sum);
            Console.WriteLine("Average = "+average);

            Console.ReadKey();
        }
    }
}

---------------------------------------------------------------------------------



Using foreach loop:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] int_arr = new int[] { 1, 2, 3, 4, 5 };

            int sum = 0;

            foreach(int i in int_arr)
            {
                sum += i;
            }

            double average = sum / int_arr.Length;

            Console.WriteLine("Sum = "+sum);
            Console.WriteLine("Average = "+average);

            Console.ReadKey();
        }
    }
}
 

---------------------------------------------------------------------------------

How to sort an int array:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] int_arr = new int[] { 1, 3, 2, 7, 6 };

            Array.Sort(int_arr);

            foreach(int i in int_arr)
            {
                Console.Write(i+"  ");
            }
            Console.ReadKey();
        }
    }
}


---------------------------------------------------------------------------------

 How to sort an string array using sort method();

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] int_arr = new char[] {'a','c','d','b','f','e'};

            Array.Sort(int_arr);

            foreach(char i in int_arr)
            {
                Console.Write(i+"  ");
            }
            Console.ReadKey();
        }
    }
}


---------------------------------------------------------------------------------


Write fibonoci series in c#:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 10;

            int f1 = 0;
            int f2 = 1;
            int f3 = 0;

            int i=0;

            Console.Write(f1+" ");
            Console.Write(f2+" ");

            for(i=0;i<count;i++)
            {
                f3 = f1 + f2;
                Console.Write(f3+" ");

                f1 = f2;
                f2 = f3;
            }
            Console.ReadKey();
        }   
    }
}

---------------------------------------------------------------------------------


How to reverse a number:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 12345;
            int reverse_num = 0;
            while(num>0)
            {
                int reminder = num % 10;

                reverse_num = (reverse_num * 10) + reminder;
                num = num / 10;
            }
            Console.Write(reverse_num);
            Console.ReadKey();
        }   
    }
}


---------------------------------------------------------------------------------

Reverse a string using array:namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "ABCDEF";
            string rev_str="";
            int length=str.Length-1;
            while(length>=0)
            {
                rev_str = rev_str + str[length];
                length--;
            }
            Console.Write(rev_str);
            Console.ReadKey();
        }
    }
}
 

---------------------------------------------------------------------------------

How to remove duplicate character from many characters:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "abcdefab";
            List<char> found = new List<char>();
            foreach (char c in s)
            {
                if (!found.Contains(c))
                {
                    found.Add(c);
                }
            }
            foreach (char i in found)
            {
                Console.Write(i);
            }
            Console.ReadKey();
        }
    }
}

---------------------------------------------------------------------------------

How to devide full name into first name, middle name and last name and show output:

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Akash Dadarao Bonde";

            string[] name = s.Split(' ');

            Console.WriteLine("First name: " + name[0] );
            Console.WriteLine("Middle name: " + name[1]);
            Console.WriteLine("Last name: " + name[2]);
            Console.ReadKey();
        }
    }
}


---------------------------------------------------------------------------------