C program to find factorial of a number

In this program, we will read and integer number and find the factorial using different methods - using simple method (without using user define function), using User Define Function and using Recursion.

What is factorial?
Factorial is the product of an integer with it's all below integer till 1. In mathematic representation factorial represents by ! sign.

For Example:
Factorial 5 is:
5! = 120 [That is equivalent to 5*4*3*2*1 =120]

Factorial program in C

Simple program - without using User Define Function

﻿

```/*C program to find factorial of a number.*/

#include <stdio.h>

int main()
{
int num,i;
long int fact;

printf("Enter an integer number: ");
scanf("%d",&num);

/*product of numbers from num to 1*/
fact=1;
for(i=num; i>=1; i--)
fact=fact*i;

printf("\nFactorial of %d is = %ld",num,fact);

return 0;
}
```

Output

```Enter an integer number: 7

Factorial of 7 is = 5040
```

User Define Function

﻿

```/*Using Function: C program to find factorial of a number.*/

#include <stdio.h>

/* function : factorial, to find factorial of a given number*/

long int factorial(int n)
{
int i;
long int fact=1;

if(n==1) return fact;

for(i=n;i>=1;i--)
fact= fact * i;

return fact;
}

int main()
{
int num;

printf("Enter an integer number :");
scanf("%d",&num);

printf("\nFactorial of %d is = %ld",num,factorial(num));

return 0;
}
```

Output

```Enter an integer number: 7

Factorial of 7 is = 5040
```

Using Recursion

﻿

```//function for factorial
long int factorial(int n)
{
if(n==1)    return 1;
return n*factorial(n-1);
}
```

Logic behind to implement these programs

• Variables for program - int to store number, long int to store factorial because factorial value is higher than integer value.
• Now, input the number.
• Run a loop from num to 1 and multiply the numbers in a variable called fact.
• And the fact will be the factorial after executing the loop.

TOP Interview Coding Problems/Challenges

Learn PCB Designing: PCB DESIGNING TUTORIAL

 Recommended posts C Tips & Tricks, C++ Tips & Tricks Introduction to Linux (Its modes, Safety, Most popular Applications) Linux Best Distros of 2018 C programming optimization techniques Differences b/w C & Embedded C? Embedded C Interview Q. & A. C programming tips for Embedded Development Basic rules of writing a C program Important points (rules) to remember while writing C/C++ program Top 5 Websites for solving programming challenges Read more...

 Others... Computer G.K. (MCQ) Most viewed pages... Categories...

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates