# Write a recursive program to print the value of factorials from 1 to N using C/C++?

480 views
Write a recursive program to print the value of factorials from 1 to N using C/C++?
posted Mar 8, 2016

this piece of code will print factorial of all number ranging from 1 to n. Complexity is O(n).

``````  #include <stdio.h>
int fact(int n)
{
static int count=1;
if(n==0)
return 1;
else
n=n*fact(n-1);
printf("factorial of %d=%d\n",count++,n);
return n;
}

int main(void) {
int n;
printf("enter the number:\n");
scanf("%d",&n);
fact(n);
return 0;
}
``````
``````#include<stdio.h>
int fact(int);
int main(){
int num,f;
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}

int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));
}
``````
``````#include<stdio.h>

int fact(int);

int main(){
int n1,f;
printf("\nEnter a number: ");
scanf("%d",&n1);
f=fact(num);
printf("\nFactorial of %d is: %d",n1,f);
return 0;
}

int fact(int n){
if(n==1)
return 1;
else
return(n*fact(n-1));
}
``````
I think the question if to find the factorial of all values from 1 to N not just N. Do you like to edit the answer.
Similar Questions

Input:
arr = {'a', 'b'}, length = 3

Output:
aaa
aab
aba
abb
baa
bab
bba
bbb

+1 vote

Write a recursive function:

``````int sum( int x, int max )
{
/* complete the code */
}
``````

that calculates the sum of the numbers from x to max (inclusive). For example, sum (4, 7) would compute 4 + 5 + 6 + 7 and return the value 22.

Note: The function must be recursive.