-2

Everyone in the solutions are giving functions with if else statements and return statement which i don't want . I have clearly mentioned that there should be no if else and return statement . that's why in my program i also didn't use it

I want to know if we can write the factorial of a number using recursion without using any if-else statements and a return statement. If yes, then how?

I tried something like this:

int n;
int fact=1;
factorial(){
    while(n){
        fact= fact * n;
        n--; factorial();
    }
}
main(){
    n = 5;
    factorial();
    printf("%d",fact);
}

The above program is correctly giving a factorial of the number but the recursive call is just a dummy here. The recursive call is not actually doing anything. So is it possible to write a factorial of a number with recursion without return statement & ifelse where recursive calls are actually contributing to find the factorial

  • possible duplicate of [How to find a factorial?](http://stackoverflow.com/questions/2416483/how-to-find-a-factorial) – styvane Aug 06 '15 at 17:22
  • @user3100115: not a duplicate, question is how to do it recursively without if-else – Veltas Aug 06 '15 at 17:35
  • @Veltas YES IT IS! some of the answers did not use if-else. there is even same answer as yours – styvane Aug 06 '15 at 17:41
  • 2
    Recursive but without `return`? This requirement is nonsensically arbitrary. Why is `return` prohibited? – AnT stands with Russia Aug 06 '15 at 17:56
  • @user3100115 Only one I saw is using `tgamma` which kind of misses the point. – Veltas Aug 06 '15 at 17:57
  • Guys there should not be any return statement and no if else is allowed i know how to find the factorial of a number but thats not a problem the problem is can you write a recursive function to find factorial without using return statement and if else – Himanshu Kaushik Aug 06 '15 at 18:08
  • 1
    Unclear what you want, if is literally without `if`-`else` then is easy, if is literally without any logic then is conceptually impossible. – Veltas Aug 06 '15 at 18:20
  • Just use if-else. It is supported by all compilers. There is no reason not to use it. – Raymond Chen Aug 07 '15 at 02:29

4 Answers4

4
int result;

void factorial(int n)
{
  (result = 1, n <= 1) || (factorial(n - 1), result *= n);
}

int main()
{
   factorial(5);
   printf("%d\n", result);
}

Or, better

void factorial(int *n)
{
  int f;
  (f = 1, *n <= 1) || (f = (*n)--, factorial(n), f *= *n);
  *n = f;
}

int main()
{
   int n = 5;
   factorial(&n);
   printf("%d\n", n);
}

Or, if ?: is allowed the last one can be rewritten without that || trickery

void factorial(int *n)
{
  int s;
  *n = *n <= 1 ? 1 : (s = (*n)--, factorial(n), *n * s);
}
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

I think you are using C in a wrong way, because to use the global variable n is wrong practice. It would have to be a parameter of the function factorial.
Once this issue is fixed, we can handle the problem you are asking for.

I see unnecessary to restrict ourselves to "not use" if-else sentences.
Anyway, it's easy to do it in C, since we have the ternary operator.
For example, the following:

char c = (3<4)? 'y': 'n';  

is equivalent to:

char c;
if (3<4)
  c = 'y';
else
  c = 'n';

In the case of recursive factorial, we would have:

 int factorial(int n) {
    return (n <= 1)? 1: factorial(n-1);
 }
pablo1977
  • 4,281
  • 1
  • 15
  • 41
  • I have clearly specified that there should not be any if else statement and return statement i know how to find a factorial of a number with or without recursion but this is not my solution – Himanshu Kaushik Aug 06 '15 at 18:11
  • In the first place, I ommited the part of the text you don't want return statements. On the other hand, functions without return statements? I'm sorry, but this is not C. – pablo1977 Aug 06 '15 at 18:25
  • I know we have a turnery operator i c for If.. else... I am programming from quite some time and yes it is possible to write a factorial function which is using recursion without any return statement & if else – Himanshu Kaushik Aug 06 '15 at 19:02
0

Using a recursive helper function:

int helperfact(int *m, int *n){
    *n>0 && ( *m *= (*n)--, helperfact(m, n)); 
}

void fact(int n){
    int m = 1;
    helperfact(&m,&n);
    printf("%d\n",m);
}

int main(void){
   fact(10);
   fact(0);
   return 0;
}

Output:

3628800
1
John Coleman
  • 51,337
  • 7
  • 54
  • 119
-1

Your could try this:

int factorial (int t) /*t = factorial*/
{ 
    int r; /*result*/
    if (t == 1) /*if t = 1 the recursion will end*/
    { 
        return 1; 
    } 
    r = t * factorial(t - 1) ; /*calling the function again with times - 1*/
return r;                      /*will make recursion until t = 1          */
}

Hope it works.