Skip to main content

FACTORIAL USING RECURSION

Factorial using recursion

#include < stdio.h >

long factorial(int);

int main()
{
  int n;
  long f;

  printf("Enter an integer to find factorial\n");
  scanf("%d", &n);

  if (n < 0)
  printf("Negative integers are not allowed.\n");
  else
  {
  f = factorial(n);
  printf("%d! = %ld\n", n, f);
  }

  return 0;
}

long factorial(int n)
{
  if (n == 0)
  return 1;
  else
  return(n * factorial(n-1));
  /*recursive call to factorial function*/
}

Comments

Popular posts from this blog

Software

Software The ingredients that enables a computer to perform a specific task is software, Which consist​ of instructions.A set of instructions that drive a computer to perform specific task is called program.These instructions tell the machine physical components what to do,without the instruction a computer couldn't do anything at all.Software that are classified into two category 1.System software 2.Application software

REVERSE NUMBER

Reverse number #include < stdio.h > int main() {   int n, reverse = 0;   printf("Enter a number to reverse\n");   scanf("%d",&n);   while (n != 0) {   reverse = reverse * 10;   reverse = reverse + n%10;   n = n/10; } /*taking unit place digit of no and moving to reverse Dividing the no to discard unit place digit*/   printf("Reverse of entered number is = %d\n", reverse);   return 0; }

REVERSE NUMBER USING ARRAY

Reverse number using array #include < stdio.h > void main() { int n, array[100], i; printf("Enter no of digits in your number"); scanf("%d",&n); printf("Enter no.") for(i=0,i < n;i++) scanf("%d",&a[i]); /*storing each no in array*/ printf("Reverse no is : "); for(i=n-1,i > =0;i--) printf("%d",a[i]); /*Printing array in reverse order*/ }