Q3 Write a window application in C# to find out the factorial of given number and input should be taken from the user.

We know that factorial means if we input number 5 then we have to calculate this number like 1*2*3*4*5 and then the final calculation will be the factorial. The form is as follows:

factorial-numbers

Here textbox accept number and first it will be converted into integer number and assigned to the variable n. the code is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CFactorial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int n = Convert.ToInt32(textBox1.Text);
int i, fact = 1;
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
MessageBox.Show("Factorial is : "+fact,"Factorial");
}
}
}


Here we have used for loop. Here we used condition i<=n means this loop will be continue till the ith value will not be equal to the entered number. Inside the loop, we have used fact*I and it is assigned to fact after that we have incremented the value of variable I by 1. This loop will be continued till the condition will not become false. The output is as follows:

factorial-numbers