Responsive Ads Here

Sunday, October 13, 2019

SIMPLE PROGRAM FOR FACTORIAL OF A NUMBER IN C-LANGUAGE

SIMPLE PROGRAM FOR FACTORIAL OF A NUMBER IN C-LANGUAGE


Factorial Program in C (using for loop)

This is the simple program for factorial of a number in C-language  To find factorial we are using  for loop.

Firstly factorial is represented by a  '!' and it is calculated by decending the number of which you want to find the factorial of, so the factorial of a positive number n is given by 
n! = n*(n-1)*(n-2)*(n-3)...1

FOR EXAMPLE
3! = 3*2*1 = 6
5! = 5*4*3*2*1 = 120  






Program: 

#include
#include

void main(void)
{
     clrscr();
     int n,i,fact=1;
     printf("Program for factorial of a number   ");
     scanf("%d",&n); // input from user
     for(i=n;i>=1;i--)
     {
fact =fact*i;
     }
     printf("The factorial is = %d",fact);
     getch();
}


OUTPUT:

No comments:

Post a Comment