Sunday, 15 December 2013

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:-


C# program to swap two numbers without using 3rd variable



In this article we will discuss with you the program for swapping two numbers without using third variable using console application in c#.net:

Step 1: Take a new console application using c# as a language in the Visual studio.
Step 2: Declare 2 variable  int_First_No, int_Second_No and take the 2 input numbers in these variables for swapping. 
Step 3: now add the 2 variable and assign it to the “int_First_No”, after this minus the “int_Second_No” from this number we will get the first variable no in the Second variable. Likewise minus the “int_First_No” from this number we will get the Second variable no in the first variable. In this way two numbers gets swapped without use of the third number.

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

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

            int int_First_No, int_Second_No;

            Console.WriteLine("please the enter the two numbers for swapping");
            int_First_No = int.Parse(Console.ReadLine());
            int_Second_No = int.Parse(Console.ReadLine());

            Console.WriteLine("Before swapping the two numbers is " + int_First_No + " and " + int_Second_No);

            int_First_No = int_First_No + int_Second_No;
            int_Second_No = int_First_No - int_Second_No;
            int_First_No = int_First_No - int_Second_No;

            Console.WriteLine("After swapping the two numbers is " + int_First_No + " and " + int_Second_No);
            Console.ReadLine();
        }
    }
}


Output:



C# program to test number is Even or Odd no



In this article we will discuss with you the program for testing a number for even and odd number using console application in c#.net using :

Step1: Take a new console application using c# as a language in the Visual studio.
Step2: Declare a variable “int_Number” for storing the value for checking whether it is even or odd.
Step3: now take the modulo of 2 of that number, if it result in zero that means it is even no otherwise it is odd no.


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

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

            int int_Number;
           
            Console.WriteLine("please the numbers to be tested for even and odd number");
            int_Number = int.Parse(Console.ReadLine());


            if (int_Number % 2 == 0)
            {
                    Console.WriteLine("Entered number is an Even number.");
            }
            else
            {
                    Console.WriteLine("Entered number is an Odd number.");
            }
            Console.ReadLine();


        }
    }
}

Output :