i need this in c program please

Computing Factorials

Lab #2

Name your program lab2.c

Start by asking the user to enter a number

between 1 and 15

Check if the user has complied.

Keep asking the user for a number between 1

and 15 until the user complies.

Then ask if recursion is desired or not.

If recursion is not desired call a function to calculate

N! (where N is the number entered)



If recursion is required call a recursive function to

calculate N! (where N is the number entered)

Respuesta :

Answer:

C code is explained below

Explanation:

#include <stdio.h>

// recursive function to calculate factorial

long recurivefact(int n) {

if (n>=1)

return n*recurivefact(n-1);

else

return 1;

}

// iterative function to calculate to factorial

long fact(int n){

int fact=1,i;

for (i = 1; i <= n; i++) {

fact =fact*i;

}

return fact;

}

int main()

{

int n;

char ch,choice='N';

// if choice is 'N' or 'n' keep running

while(choice=='N' || choice=='n'){\

// take user input of N

printf("Enter a number between 1 and 15:\n");

scanf("%d",&n);

 

// ask if user need the answer using recursive function or iterative function

printf("recursion is desired or not (Y/N):\n");

scanf(" %c", &ch);

// if ch is 'Y' or 'y' call recursive function else iterative function

if(ch=='Y' || ch=='y')

printf("%ld\n",recurivefact(n));

elsel

printf("%ld\n",fact(n));

// ask the user if he want to continue compiling(running) or not

printf("Do you wish stop compiling (Y/N):\n");

scanf(" %c",&choice);

}

return 0;

}

ACCESS MORE