top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

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

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+4 votes

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;
    }
answer Mar 20, 2016 by Shahsikant Dwivedi
0 votes
#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));
 }
answer Mar 9, 2016 by Manikandan J
0 votes
#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));
 }
answer Mar 9, 2016 by Ashish Kumar Khanna
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
+2 votes

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.

...