• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Monday, August 10, 2020

Factorial Program in C# with Examples

 Admin     August 10, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss the Factorial Program in C# with some examples. Please read our previous article where we discussed the Armstrong Number Program in C# with examples. In many interviews, the interviewer asked this question in many different ways. As part of this article, we are going to discuss the following pointers.
  1. What is the factorial of a number?
  2. Factorial of a number using for loop, while loop and do-while loop in C#.
  3. Factorial of a number using the recursive function in C#.
What is Factorial of a number?
The Factorial of a number (let say n) is nothing but the product of all positive descending integers of that number. Factorial of n is denoted by n!. Please have a look at the following image which shows how to calculate the factorials of a number.
Factorial Program in C# with Examples

Factorial Program in C# using for loop:
In the following example, we take the number from the console and then calculate the factorial of that number using for loop.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a Number : ");
            int number = int.Parse(Console.ReadLine());
            int factorial = 1;
            for (int i = 1; i <= number; i++)
            {
                factorial = factorial * i;
            }
            Console.Write($"Factorial of {number}  is: {factorial}");
            
            Console.ReadLine();
        }
    }
}
Output:
Factorial Program in C# using for loop:

Factorials of a number using while loop in C#:
In the following example, we use while loop to calculate the factorial of a number.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a Number : ");
            int number = int.Parse(Console.ReadLine());
            long factorial = 1;
            while (number != 1)
            {
                factorial = factorial * number;
                number = number - 1;
            }
            
            Console.Write($"Factorial is: {factorial}");           
            Console.ReadLine();
        }
    }
}
Output:
Factorials of a number using while loop in C#

Factorial of a number using Recursive Function in C#:
In the following program, we use a recursive function to calculate the factorial of a given number.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a Number : ");
            int number = int.Parse(Console.ReadLine());
            long factorial = RecursiveFactorial(number);
            Console.Write($"Factorial of {number} is: {factorial}");    
            
            Console.ReadLine();
        }
        static long RecursiveFactorial(int number)
        {
            if (number == 1)
            {
                return 1;
            } 
            else
            {
                return number * RecursiveFactorial(number - 1);
            }    
        }
    }
}
Output:
Factorial of a number using Recursive Function in C#

Factorial of a number using the do-while loop in C#:
In the below program, we use the do-while loop to calculate the factorial of a given number. The number here we are taking from the console.
using System;
namespace LogicalPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a Number : ");
            int number = int.Parse(Console.ReadLine());
            long factorial =  1;
            do
            {
                factorial = factorial * number;
                number--;
            } while (number > 0);
            Console.Write($"The Factorial is: {factorial}");
            Console.ReadLine();
        }      
    }
}
Output:
Factorial of a number using do-while loop in C#

In the next article, I am going to discuss the Sum Of Digits Program in C# with some examples. Here, in this article, I try to explain the different ways to implement the Factorial Program in C# with examples.

Summary:
I hope this post will be helpful to write a Factorial Program in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Navigation Menus in ASP.NET Core
    In this article, I am going to discuss how to create Responsive Navigation Menus in ASP.NET Core Application using bootstrap and JQuery. P...
  • Web API Attribute Routing Route Constraints
    In this article, I will discuss the Web API Attribute Routing Route Constraints with examples. We are going to work with the same example ...
  • How to use Swagger in Web API Application
    In this article, I am going to discuss how to use Swagger in WEB API Application to document and test restful Web API services. Please rea...
  • Routing and Action Selection in Web API
    In this article, I am going to discuss Routing and Action Selection in Web API i.e. how the ASP.NET Web API Framework routes or transfer a...
  • Bubble Sort in C#
    In this article, I am going to discuss the Bubble Sort in C# with Examples. The Bubble sort is a sorting algorithm and used by the many de...
  • ViewStart in ASP.NET Core MVC
    In this article, I am going to discuss the ViewStart in ASP.NET Core MVC Application. Please read our previous article before proceeding t...
  • Refresh Token in Web API
    In this article, I am going to discuss how to implement Refresh Token in Web API by validating the clients, as well as I, will also discus...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape