DIGIT-ARC read-only archive · snapshot: July 23, 2026

factorial

12 posts · originally from https://geek.digit.in/community/threads/factorial.108527/

Pragadheesh
Pragadheesh Original post #1
Feb 12, 2009 at 9:39 AM
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.!!
Liverpool_fan
Feb 12, 2009 at 11:26 AM
Recursion.
*en.wikipedia.org/wiki/Recursion_(computer_science)

Why NOT use a loop ? :confused:

For learning Recursion I think.

EDIT: Though I admit even in recursion you have to exit it as well using conditions.
S
Feb 12, 2009 at 1:42 PM
But even for recursion, we need both if statement and * operators...

Arun
Pragadheesh
Feb 12, 2009 at 2:01 PM
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 at 3:22 PM
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...
D
Feb 12, 2009 at 9:22 PM
In which programming language do you want answer?
Pragadheesh
Feb 16, 2009 at 6:28 PM
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.!
sam_52136
sam_52136 #10
Feb 16, 2009 at 6:47 PM
The first one is too good man..!
but what if some one asks you for factorial of 20 ?
hehe..
Pragadheesh
Feb 16, 2009 at 9:17 PM
@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 at 8:33 AM
Can i have a hint...