Calculations

Program for calculating factorial of a number using recursion in C++

Program for calculating the factorial of a number using recursion

In this tutorial, we will discuss the Program for calculating the factorial of a number using recursion

There are many ways to calculate factorial in the Java language. and one of this given below

 

In this article, we are going to learn how to calculate factorial of a number using the recursive function  in C++ programming language

Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one

 

Example

factorial of 5 is

5!=5*4*3*2*1=120

 

factorial of 4 is

4!=4*3*2*1=24

 

factorial of n is

n!=n*(n-1)*....2*1
Factorial of nth number

Program

This program allows the user to enter a positive integer number and it calculates the factorial of the given number using the recursive function in C++ language

#include <iostream>
#include <conio.h>
using namespace std;

int factFind(int);//function prototype
int main()
{
    int num;
    //ask input from the user
    cout<<"Enter a positive integer: ";
    //given value is stored in num variable
    cin>>num;

    //calling the findFact() function- user-defined
    int factorial=factFind(num);

    //displaying factorial of the given number
    cout<<"factorial of "<<num<<" is: "<< factorial;
    getch();
    return 0;
}
int factFind(int num){//function definition
if(num>=1)
    //function calling itself recursively
    return num*factFind(num-1);
else
    return 1;
}

When te above code is executed, it produces the following result

Enter a positive integer:5

factorial of 5 is: 120

 

Approach

  • The program requests to enter a number from the user
  • The entered number  is stored in the num variable
  • when The findFact() is called from the main method, the num(user input) is passed as an argument to the function findFact()
  • If the num(given number) is greater than or equal to 1, the number is multiplied with the result also  the num(num-1) is passed as an argument again
  • In each and every recursive call, the value of the argument num is decreased by 1(num-1) until the num becomes less than 1(num>=1)
  • When the value of num is less than 1, The control skips the recursive function and comes to end the recursive call

 

To clearly understand this article, you should have the previous knowledge of the following C programming subject.

The recursive function in C++ language

If statements in C++ language

Function in C++ language

 

Similar post

Find factorial of a number  in Java

Find factorial of a number  in C language

Find factorial of a number  in C++ language

Find factorial of a number  in Python language

Find factorial of a number  in Java using method

Find factorial of a number  in C using the function

Find factorial of a number  in C++ using the function

Find factorial of a number  in Python using the function

Find factorial of a number  using the pointer in C language

Find factorial of a number  using the pointer in C++ language

Find factorial of a number  using the recursion in Java language

Find factorial of a number  using the recursion in C language

 

Program to calculate factorial of a number using recursion in Java
C Program for calculating factorial of a number using recursion
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago