Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
127 views
in Functions by (29.6k points)
closed by

The factorial of a number, say N is the product of first N natural numbers. Thus, factorial of 5 can be obtained by taking the product of 5 and factorial of 4. Similarly factorial of 4 be found out by taking the product of 4 and factorial of 3. At last the factorial of 1 is 1 itself. 

Which technique is applicable to find the factorial of a number in this fashion? Write a C++ function to implement this technique. Also explain the working of the function by giving the number 5 as input.

1 Answer

+1 vote
by (29.8k points)
selected by
 
Best answer

A function calls itself is known as recursion.

#include<iostream>

using namespace std; 

int fac(int); 

int main() 

int n; 

cout<<“enter a number”;

cin>>n;

cout<<fac(n); 

int fac(int n) 

{

if (n == 1) return (1); 

else 

return (n × fac(n – 1)); 

}

The working of this program is as follows If the value of n is 5 then it calls the function as fa. The function returns value 5 × fac(4), That means this function calls the function again and returns 5 ×4 × fac(3). This process continues until the value n = 1. So the result is 5 × 4 × 3 × 2 × 1 = 120.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

...