Search C Program | nimishsoft@gmail.com

Factorial by Recursion

/* Factorial by Recursion */

#include<stdio.h>
int fact(int);

int main()
{
  int n;
  printf("Type any value : ");
  scanf("%d",&n);
  n=fact(n);
  printf("\nFactorial : %d ",n);
  return 0;
}

int fact(int x)
{
  if(x==1)
    return(x);
  else
   x=x*fact(x-1);
}


5 comments:

  1. int fact(int x)
    {
    if(x<=1)
    return(1);
    else
    return(x*fact(x-1));
    }

    ReplyDelete
  2. Replies
    1. By definition 0!=1 so you can modify the line if(x==1) to if(x==0||x==1)

      Delete
  3. Q2: Display the following pattern on the text box of the Windows FORM in C#
    a) 11 22 33 44 55 66
    77 88 99 60 61
    62 63 64 65
    66 67 68
    69 70
    71
    b) 71
    69 70
    66 67 68
    62 63 64 65
    77 88 99 60 61
    11 22 33 44 55 66
    Hint: You can use two text boxes one for part A and part B separately.

    ReplyDelete