Status
Not open for further replies.

Pragadheesh

In the zone
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.!!
 
OP
Pragadheesh

Pragadheesh

In the zone
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

Sami Hyypiä, LFC legend
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...
 
OP
Pragadheesh

Pragadheesh

In the zone
i came up with 2 solutions, one of which is quite silly.
1)
Code:
#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)
Code:
#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.!
 
OP
Pragadheesh

Pragadheesh

In the zone
@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..
 
Status
Not open for further replies.
Top Bottom