- 0
Notifications Mark All Read
- Login
- Get Prime
Program for Factorial Using Recursion
Factorial
We will write a C program for calculate the factorial and the number will be entered by the user. This will help to understand the basic structure of programming. In this program , we will display the factorial of given number easily by using proper syntax and algorithms.
Working of Program :
In the program, we will require some numbers from the user to display the factorial of given numbers.
Important Note :
To calculate the factorial, factorial is used for exponential function in power series.
- Symbol of factorial is ! and value of 0! is 1.
Problem 1
Write a program to calculate factorial of given numbers.
- Firstly, we have to enter the numbers.
- Then print the factorial.
Code
#include<stdio.h> long int multiplyNumbers (int n); int main () { int n; printf ("Enter a positive integer = "); scanf ("%d", &n); printf ("Factorial of %d = %ld", n, multiplyNumbers (n)); return 0; } long int multiplyNumbers (int n) { if (n >= 1) return n * multiplyNumbers (n - 1); else return 1; }
Output
Enter a positive integer = 5 Factorial of 5 = 120
Note:
In the following program we will display the factorial of given numbers.
Problem 2
Write a program to calculate the factorial.
- Firstly, we have to enter the number.
- Then print the factorial.
Code
#include<stdio.h> long factorial (int n) { if (n == 0) return 1; else return (n * factorial (n - 1)); } void main () { int number; long fact; printf ("Enter a number = "); scanf ("%d", &number); fact = factorial (number); printf ("Factorial of %d = %ld\n", number, fact); return 0; }
Output
Enter a number = 6 Factorial of 6 = 720
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment