Monday, December 17, 2012

PROGRAM TO CALCULATE FACTORIAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int num;
int fact=1;
Console.Write("Please enter a number: ");
String strNum = Console.ReadLine();
num = int.Parse(strNum);
if (num == 0)
{
Console.WriteLine("Factorial= 1");
}
else
{
for (int i = num; i > 0; i--)
{
fact = fact * i;
}
Console.WriteLine("Factorial= "+fact);
}
Console.ReadLine();
}
}
}

<<<<<<<<<<<<<<<<<<<<<< OUTPUT >>>>>>>>>>>>>>>>>>>>>>>>>>>

Please enter a number: 0

Factorial= 1
Please enter a number: 1
Factorial= 1
Please enter a number: 2
Factorial= 2
Please enter a number: 5
Factorial= 120


No comments:

Post a Comment