Sunday, 15 December 2013

Getting started with Console Application


In order to getting started with the console application first start the Visual Studio 2010.

Then click on New Project…  as shown below:


Select the Console Application and write the name of the Console Application as you want and then click on OK.

Now by using this following code we can write our first program to print the “Hello World”.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hello_World
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Hello World");
            Console.ReadLine();
        }
    }
}


Output:


C# program to check the character is vowel or not



In this article we will discuss with you the program for checking whether character is vowel or not using console application in c#.net using for loop:
Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Declare a variable “str_Name” and take the input of the character which you want to test.
Step3: Now check if it is anything from the A,E,I,O,U then it is a vowel otherwise it’s not vowel.


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

namespace vowel
{
    class Program
    {
        static void Main(string[] args)
        {
            string str_Name;

            Console.WriteLine("Please enter The charcater to check whether it is vowel or not");
            str_Name = Console.ReadLine();


            if ((str_Name == "A") || (str_Name == "E") || (str_Name == "I") || (str_Name == "O") || (str_Name == "U")
                || (str_Name == "a") || (str_Name == "e") || (str_Name == "i") || (str_Name == "o") || (str_Name == "u"))
            {
                Console.WriteLine("The charcater is vowel");
            }
            else
            {
                Console.WriteLine("The charcater is not vowel");
            }

            Console.ReadLine();
        }
    }
}
Output:-


C# program to check whether the number is Armstrong no or not



In this article check whether the input number is Armstrong no or not by using console application in c#.net using while loop:
Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Declare the variables int_Number, int_Temp, int_Remainder, int_Sum and initialize the “int_sum” value to 0.
Step3: Read an input from the User in this Variable “int_Number” for storing that number which you want to test for Armstrong number, now assign that value to “int_Temp”.
Step4: now run the while loop till the value of “int_Temp” doesn’t become 0 and then digit by digit take the values and multiply that digit value three times and then add it to the “int_sum” variable and in the next step  divide the “int_Temp” variable by 10. In the end check if the “int_sum” value is equal to the “int_Number” then it means that it is Armstrong number.

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

namespace Armstrong_Number
{
    class Program
    {
        static void Main(string[] args)
        {

            int int_Number, int_Temp, int_Remainder, int_Sum;
            int_Sum = 0;
            Console.WriteLine("please the numbers to be tested for Armstrong number");
            int_Number = int.Parse(Console.ReadLine());

            int_Temp = int_Number;


            while (int_Temp != 0)
            {
                int_Remainder = int_Temp % 10;
                int_Sum = int_Sum + int_Remainder * int_Remainder * int_Remainder;
                int_Temp = int_Temp / 10;
            }

            if (int_Number == int_Sum)
            {
                Console.WriteLine("Entered number is an armstrong number. ");
            }
            else
            {
                Console.WriteLine("Entered number is not an armstrong number. ");
            }
            Console.ReadLine();

        }
    }
}

Output:


C# program to check whether given no. is prime or not



In this article we will discuss with you the program for checking whether given no. is prime or not using console application in c#.net:
Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Read an input from the User in this Variable “int_Number” for storing that number which you want to test for prime number.
Step3: Now if that input number is 1 or 2 then it will be a prime number otherwise we have to loop from 2 till that number and Start dividing that input number from 2 till that number, If it’s divisible by any of the number before the “input number-1” then it means that it’s not a prime number otherwise it is a prime number.

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

namespace PrimeNo
{
    class Program
    {

        static void Main()
        {
           
            int int_Number;
            int i;
            Console.WriteLine("Enter a number to check whether it is prime or not?");

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

            if ((int_Number == 2) || (int_Number == 1))
            {
                Console.WriteLine("The Given number is prime number");
                Console.ReadLine();
            }
            else
            {
                for (i = 2; i < int_Number; i++)
                {
                    if (int_Number % i == 0)
                    {
                        Console.WriteLine("The Given number is not prime number");
                        Console.ReadLine();
                        break;
                    }
                }

                if (int_Number == i)
                {
                    Console.WriteLine("The Given number is prime number");
                    Console.ReadLine();
                }
            }
       }
    }

}

Output:-


C# program to test if string is Palindrome or not


In this article we will discuss with you the program for testing a string whether it’s palindrome or not using console application in c#.net:
Palindrome: - Palindrome is nothing but the string that remains the same when its reversed.
Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Declare 2 variable “str_Orginal_String” and “str_Reverse_String” for storing the original   and reverse string and initialize the reverse variable “str_Reverse_String” with blank.
Step3: Read an input from the User in this Variable “str_Orginal_String” for storing that string which you want to test for palindrome.
Step4: now loop through the last position to the first position of that Original string and store each character value in “str_Reverse_String” to get the final Reverse string.

Step5: Now compare the original string with the reverse string if that is same then it would be palindrome string otherwise it will be not palindrome string.

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

namespace Palindrome
{
    class Program
    {

        static void Main()
        {
           
            string str_Orginal_String, str_Reverse_String;
            str_Reverse_String = "";
            Console.WriteLine ("Please enter the string to test for palindrome string ?");

            str_Orginal_String = Console.ReadLine ();

            for (int i = str_Orginal_String.Length - 1; i >= 0; i--)
            {
                str_Reverse_String = str_Reverse_String + str_Orginal_String[i];
            }

            if (str_Orginal_String == str_Reverse_String)
            {
                Console.WriteLine ("The string " + str_Orginal_String + " is palindrome");
            }
            else
            {
                Console.WriteLine ("The string " +str_Orginal_String + " is not                   palindrome");
            }
            Console.ReadLine ();
       }
    }



}

Output:-


C# program to find the Fibonacci Series



In this article we will show you how to write code for Fibonacci series using console application in c#.net using for loop:

Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Declare a variable “int_No_of_term” for getting the Number of Term in the Fibonacci series.
Step3: Read an input from the User in this Variable.
Step4: Make an array for name “int_fibbo” of the size “int_No_of_term” for storing the fibonacci series values.
Step5: Assign the first and second value of the Fibonacci series in the array position 0 and 1 respectively. Print the first 2 number of the Fibonacci series.
Step6: now loop through till the last no in the series to calculate the next Fibonacci numbers using the logic nth series value= (n-1)th series value+ (n-2)th series value.

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

namespace Fibonaci
{
    class Program
    {

        static void Main()
        {
           
            int int_No_of_term;
            Console.WriteLine("Enter the number of terms for the fibonacci series you    
                           want to print?");
      
            int_No_of_term = int.Parse(Console.ReadLine());

            int[] int_fibbo = new int[int_No_of_term];

            int_fibbo[0] = 0;
            int_fibbo[1] = 1;


            //for printing the first value of the fibonacci series
            Console.WriteLine(int_fibbo[0]);
            //for printing the second value of the fibonacci series
            Console.WriteLine(int_fibbo[1]);

            for (int i = 2; i < int_No_of_term; i++)
            {
                int_fibbo[i] = int_fibbo[i - 1] + int_fibbo[i - 2];
                //for print the fibonacci series from 3rd position till nth position
                Console.WriteLine(int_fibbo[i]);
            }
            Console.ReadLine();
       }
    }



}

Output:-




C# program to find the factorial of a number



In this article we will discuss with you the program for calculating the factorial of a number using console application in c#.net:
Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Read an input from the User in this Variable “int_Number” for storing that number whose factorial you want to find out.
Step3: Now if that input number is 1 or 0 then its factorial will be 1 otherwise we have to loop from the input number till 1 and multiply the input no with its immediate lower number till 1.

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

namespace PrimeNo
{
    class Program
    {

        static void Main()
        {
           
            int int_number;
            long ln_num;
            ln_num = 1;


            Console.WriteLine("please enter the number whose factorial you want to find");
            int_number = int.Parse(Console.ReadLine());

            if ((int_number == 1) || (int_number == 0))
            {
                Console.WriteLine("factorial of the number "+int_number+ " is 1.");
            }
            else
            {
                for (int i = int_number; i >= 1; i--)
                {
                    ln_num = ln_num * i;
                }
                Console.WriteLine("factorial of the number " + int_number + " is " +ln_num );
            }

            Console.ReadLine();
       }
    }

}

Output:-


C# program for Getting First N Prime Number


In this article we will discuss with you the program for getting the first N prime number using console application in c#.net:

Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Read an input from the User in this Variable “int_Number” for storing how many prime number you want to generate.
Step3: Now if that input number is 1 then the simply prime number will be 1 or if input number is 2 then the prime number will “1 and 2” otherwise we have to compute it through the below step

  • We will test the values starting from 3 and loop till we get the “prime no count” is equal to the “input number”.
  • Here I have assigned “int_PrimeNo_Count” to 2 since 1 and 2 is first two prime nos.
  •  Now we have to loop from 2 till that number which we are testing for prime number and Start dividing that number from 2 till that number, If it’s divisible by any of the number before that i.e. “number-1” then it means that it’s not a prime number otherwise it is a prime number.
  • If it is prime number then increment the “int_PrimeNo_Count” and add it to the list of prime no in the string variable “str_PrimeNO”.




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

namespace First_N_Prime
{
    Class Program
    {

        static void Main()
        {
            int int_Number;
            int i, int_PrimeNo_Count;
            string str_PrimeNO;
            str_PrimeNO = "1 2";
            Console.WriteLine("Please enter how many prime no you want to print?");

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

            if (int_Number == 1)
            {
                str_PrimeNO = "1";
            }
            else if (int_Number == 2)
            {
                str_PrimeNO = "1 2";
            }
            else
            {
                //Start dividing the value from 3 i.e. the value of j and increment j
                 // value till we get  int_PrimeNo_Count = int_Number    

                int j = 3;

                //we have 1 and 2 as prime no hence count is 2 and increment it when you  
                 // get the next prime no
                int_PrimeNo_Count = 2;

                //till you have n no of prime number
                while (int_PrimeNo_Count != int_Number)
                {
                    for (i = 2; i <= j; i++)
                    {
                        //Divide that number starting with 2 till that number
                        if (j % i == 0)
                        {
                            break;
                        }
                    }

                    //if it is the last number then means that it's a prime no,
                    //add it to the prime no list and increment the primeNo counter
                    if (j == i)
                    {
                        str_PrimeNO = str_PrimeNO + " " + i;
                        int_PrimeNo_Count++;
                    }
                    j++;
                }
            }
           
            Console.WriteLine(str_PrimeNO);
            Console.ReadLine();

        
  
        }
    }

}

Output:-