Sunday 17 May 2015

How to calculate Factorial of a Integer number in c# ?

In this post I wrote a program in c# to calculate a factoral of Integer number using Recursive function and while loop also.

What is Recursive Function:-

A Method can call another methods but it can also call itself. When a mathod calls itself, it'll be named recursive method.
A Recursive usuallly, has the two specifications:

  • Recursive method calls itself so many times until being satisfied.
  • Recursive method has parameter(s) and calls itself with new parameter values.
Source code using Recursive function :-


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

namespace ConsoleDemoApp
{
        class Program
        {
           static void Main(string[] args)
           {
            Console.WriteLine("Enter a integer number");
            int number = Convert.ToInt32(Console.ReadLine());
            if (number <= 0)
            {
                Console.WriteLine("Please enter a positive integer number!");
                Console.ReadKey();
                return;
            }
            long fact = GetFactorial(number);
            Console.WriteLine("{0} factorial is {1}", number, fact);          
            Console.ReadKey();
           }

            // get factorial of integer
         private static long GetFactorial(int number)
         {         
            if (number == 0)
            {
                return 1;
            }
            return number * GetFactorial(number-1);
         }
    }
}


Output:-













Source code using While Loop :-


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

namespace ConsoleDemoApp
{
        class Program
        {
           static void Main(string[] args)
           {
            Console.WriteLine("Enter a integer number");
            int number = Convert.ToInt32(Console.ReadLine());
            if (number <= 0)
            {
                Console.WriteLine("Please enter a positive integer number!");
                Console.ReadKey();
                return;
            }
            long fact = Factorial(number);
            Console.WriteLine("{0} factorial is {1}", number, fact);          
            Console.ReadKey();
           }

            // get factorial of integer using while loop

          static long Factorial(int input)
           {
               int answer = 0, count = 0;
               if (input > 0)
               {
                   count = 1;
                   while (count <= input)
                   {
                       if (count == 1)
                       {
                           answer = 1;
                           count++;
                       }
                       else
                       {
                           answer = count * answer;
                           count++;
                       }
                   }
               }

               return answer;
           }
    }
}


Output:-











Source code using For Loop :-


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

namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number, fact;
            Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            if (number<0)
            {
                Console.WriteLine("Please enter a positive interger value");
                Console.ReadKey();
                return;
            }
            if (number == 0)
            {
                Console.WriteLine("\nFactorial of Given Number is: 1");
                Console.ReadLine();
                return;
            }
            fact = number;
            for (i = number - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFactorial of Given Number is: " + fact);
            Console.ReadLine();

        }
    }
}

Output:-













In the above article I try to explain how to calculate Factorial using Recursive function, while loop and for loop. So I hope its useful to you. Please post your feedback, question, or comments about this article.

Difference between Stored Procedure and Function in SQL Server

In my previous post I explained Stored Procedure and Function in details. So in this post I wrote the differences between Stored Procedure and Function in sql server. If need to learn Stored Procedures and Function so please see my previous post.

What is Stored Procedure in breif:-
A stored procedure is a group of sql statements that has been created and stored in the database. Stored procedure will accept input parameters so that a single procedure can be used over the network by several clients using different input data. Stored procedure will reduce network traffic and increase the performance. If we modify stored procedure all the clients will get the updated stored procedure.

What is Function in breif:-
Function is a database object in Sql Server. Function is compiled and executed every time when it is called. Basically it is a set of sql statements that accepts only input parameters, perform actions and return the result. Function can return only single value or a table. We can’t use function to Insert, Update, Delete records in the database table(s).


Difference between Stored Procedure and Function:-

Sl.No.
Stored Procedure
Function
1
Return value is optional.
Function must return a value.
2
Procedures can have input/output parameters.
Functions can have only input parameters.
3
Procedures cannot be called from Function.
Functions can be called from Procedure.
4
Procedure allows SELECT as well as DML (INSERT/UPDATE/DELETE) statement in it.
Function allows only SELECT statement in it.
5
Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section.
Function can be use.
6
Stored Procedures are precompiled code.
Its compiled every time whenever we call it.
7
Stored Procedures cat not be
Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.
8
Exception can be handled by try-catch block in a Procedure whereas try-catch block.
Can not be used in a Function.
9
We can go for Transaction Management in Procedure.
we can't go in Function.





In the above article I try to explain the difference between Stored Procedure and Function in SQL. I hope its useful to you. Please post your feedback, question, or comments about this article.