factorial program in c

Let’s write a programme to find factorial of a number in C.

Step 1: First of all ,we should open turbo c/c++ and write the following code in it.

Code //

// Add preprocessor file

#include<stdio.h>

#include<conio.h>

void main()

{

// Declare variables

long int i=1,n,fact=1;

//clear screen

clrscr();

printf(“enter any no”);

//asign value in variable n

scanf(“%li”,&n);

for(i=1;i<=n;i++)

{

fact=fact*i;

}

// print the result

printf(“%li”,fact);

getch();

}

1

Step 2: Save it by pressing F2 or click on File and then click on Save As then name it Fact.CPP and click OK.

2

Step 3: Now, you have to compile it by click on compile or by pressing ALT +F9 for find errors and correct them.

Step 4: After compile the code, press Alt+R to run the program and see output.

Output

3

Tags: ,