 |
12-02-2009, 09:39 AM
|
#1 (permalink)
|
|
In The Zone
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
|
factorial
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.!!
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
12-02-2009, 10:03 AM
|
#2 (permalink)
|
|
The Smaller Bang
Join Date: Sep 2007
Location: Gautham City
Posts: 7,492
|
Re: factorial
Why NOT use a loop ?
__________________
http://TheSmallerBang.wordpress.com
eMachines E725 - T4400 2.2GHz, 1GB, 160GB
Nokia 5130XM * T-Sonic 610 2GB
Nokia 2323C * Samsung Galaxy Y
Apple iPad 2 16GB WiFi
|
|
|
12-02-2009, 11:26 AM
|
#3 (permalink)
|
|
Sami Hyypiä, LFC legend
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
|
Re: factorial
Recursion.
http://en.wikipedia.org/wiki/Recursi...puter_science)
Quote:
Originally Posted by MetalheadGautham
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.
Last edited by Liverpool_fan; 12-02-2009 at 03:08 PM.
Reason: Automerged Doublepost
|
|
|
12-02-2009, 01:42 PM
|
#4 (permalink)
|
|
Human Spambot
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
|
Re: factorial
But even for recursion, we need both if statement and * operators...
Arun
|
|
|
12-02-2009, 02:01 PM
|
#5 (permalink)
|
|
In The Zone
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
|
Re: factorial
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...!!
|
|
|
12-02-2009, 03:22 PM
|
#6 (permalink)
|
|
Sami Hyypiä, LFC legend
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
|
Re: factorial
Quote:
Originally Posted by sakumar79
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...
|
|
|
12-02-2009, 09:22 PM
|
#7 (permalink)
|
|
Right Off the Assembly Line
Join Date: Feb 2009
Posts: 2
|
Re: factorial
In which programming language do you want answer?
|
|
|
12-02-2009, 09:25 PM
|
#8 (permalink)
|
|
Sami Hyypiä, LFC legend
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
|
Re: factorial
Quote:
Originally Posted by dearboy009
In which programming language do you want answer?
|
Quote:
Originally Posted by Pragadheesh
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...!!
|
.
|
|
|
16-02-2009, 06:28 PM
|
#9 (permalink)
|
|
In The Zone
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
|
Re: factorial
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.!
|
|
|
16-02-2009, 06:47 PM
|
#10 (permalink)
|
|
Sammy..
Join Date: Jan 2008
Posts: 21
|
Re: factorial
The first one is too good man..!
but what if some one asks you for factorial of 20 ?
hehe..
__________________
Sammy...
http://www.samzfunzone.blogspot.com
|
|
|
16-02-2009, 09:17 PM
|
#11 (permalink)
|
|
In The Zone
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
|
Re: factorial
@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..
|
|
|
17-02-2009, 08:33 AM
|
#12 (permalink)
|
|
Dreaming Future
Join Date: May 2005
Location: \internet\home
Posts: 177
|
Re: factorial
Can i have a hint...
__________________
World without Computer is like a man without Brain.....
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|