# factorial



## Pragadheesh (Feb 12, 2009)

hi,
how to find factorial of a number(from 1 to 10) without using,
>loop statements like for, while, do while.
>conditional operators like if, case.
>arithmetic operators like   + ,  -  ,  *  ,  %  ,  /, ++, --.??!!

is there anyway to do so using logical operators.!!


----------



## MetalheadGautham (Feb 12, 2009)

Why NOT use a loop ?


----------



## Liverpool_fan (Feb 12, 2009)

Recursion.
*en.wikipedia.org/wiki/Recursion_(computer_science)



MetalheadGautham said:


> Why NOT use a loop ?



For learning Recursion I think.

EDIT: Though I admit even in recursion you have to exit it as well using conditions.


----------



## sakumar79 (Feb 12, 2009)

But even for recursion, we need both if statement and * operators...

Arun


----------



## Pragadheesh (Feb 12, 2009)

i found this question in a C aptitude paper..!! its just you are given these constraints and to find the solution such that it satisfies them...!!


----------



## Liverpool_fan (Feb 12, 2009)

sakumar79 said:


> But even for recursion, we need both if statement and * operators...
> 
> Arun



Yeah you are right.

I guess I don't have any idea then...

I hope someone comes with an answer, I am really curious of the answer, assuming the question is not wrong...


----------



## dearboy009 (Feb 12, 2009)

In which programming language do you want answer?


----------



## Liverpool_fan (Feb 12, 2009)

dearboy009 said:


> In which programming language do you want answer?





Pragadheesh said:


> i found this question in a *C* aptitude paper..!! its just you are given these constraints and to find the solution such that it satisfies them...!!


.


----------



## Pragadheesh (Feb 16, 2009)

i came up with 2 solutions, one of which is quite silly.
1) 
	
	



```
#include<stdio.h>

int factorial(int n)
{
	int a[] = {1,2,6,24,120,720,5040,40320,362880,3628800};
	return a[n-1];
}
int main()
{
	int num;
	printf("Enter a number between 1 and 10: ");
	scanf("%d",&num);
	printf("%d",factorial(num));
}
```

this might be funny..!![]

2) 
	
	



```
#include <stdio.h>

int add(int a, int b)
{
int t1, t2, ab, bb, cb=0, orb=1, ans=0;

do {
	t1 = a >> 1; 
	t2 = t1 << 1;

	if (a==t2) ab=0; else ab=1;

	t1 = b >> 1;
	t2 = t1 << 1; 

	if (b==t2) bb=0; else bb=1;

	if (ab==1 && bb==1) { 
		if (cb==1) ans=ans | orb; 
		cb = 1; 
		}

	if ( ab!=bb ) { 
		if (cb==0) ans = ans | orb; 
		}

	if (ab==0 && bb==0) {
		if (cb==1) { 
		ans = ans | orb;
		cb=0;
				}
		}

	orb = orb << 1; 
	a = a >> 1;
	b = b >> 1;

	} while (a!=0 || b!=0);

if (cb==1) ans = ans | orb;

return ans;
}



int multiply(int x,int y)
{
	int result = 0, i = 0 , j=0;

	while((i=add(i,1)) <= y)
		result = add(result,x);

	return result;

}

int factorial(int x)
{
	if(x==1)
		return 1;
	else
		return multiply(x,factorial(x-1));

}


int main()
{
	int x;
	printf("Enter a number between 0 and 10: ");
	scanf("%d" , &x);
	printf("\nFactorial: %d\n" , factorial(x));
	return 0;
}
```


in the second solution i have *used if conditions and while loops* but no arithmetic operators involved.!


----------



## sam_52136 (Feb 16, 2009)

The first one is too good man..!
but what if some one asks you for factorial of 20 ?
hehe..


----------



## Pragadheesh (Feb 16, 2009)

@sam_52136
thanx and ya, we need to use separate data structures of different techniques like BigInteger in java to find factorial of any number, as most data types cant hold such big values..


----------



## kin.vachhani (Feb 17, 2009)

Can i have a hint...


----------

