Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 12-02-2009, 09:39 AM   #1 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Unhappy 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.!!
Pragadheesh is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 12-02-2009, 10:03 AM   #2 (permalink)
The Smaller Bang
 
MetalheadGautham's Avatar
 
Join Date: Sep 2007
Location: Gautham City
Posts: 7,492
Default 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
MetalheadGautham is online now  
Old 12-02-2009, 11:26 AM   #3 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: factorial

Recursion.
http://en.wikipedia.org/wiki/Recursi...puter_science)

Quote:
Originally Posted by MetalheadGautham View Post
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.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.

Last edited by Liverpool_fan; 12-02-2009 at 03:08 PM. Reason: Automerged Doublepost
Liverpool_fan is offline  
Old 12-02-2009, 01:42 PM   #4 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default Re: factorial

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

Arun
sakumar79 is offline  
Old 12-02-2009, 02:01 PM   #5 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Default 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...!!
Pragadheesh is offline  
Old 12-02-2009, 03:22 PM   #6 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: factorial

Quote:
Originally Posted by sakumar79 View Post
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...
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline  
Old 12-02-2009, 09:22 PM   #7 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2009
Posts: 2
Default Re: factorial

In which programming language do you want answer?
dearboy009 is offline  
Old 12-02-2009, 09:25 PM   #8 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: factorial

Quote:
Originally Posted by dearboy009 View Post
In which programming language do you want answer?
Quote:
Originally Posted by Pragadheesh View Post
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...!!
.
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline  
Old 16-02-2009, 06:28 PM   #9 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Default 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.!
Pragadheesh is offline  
Old 16-02-2009, 06:47 PM   #10 (permalink)
Sammy..
 
sam_52136's Avatar
 
Join Date: Jan 2008
Posts: 21
Default 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
sam_52136 is offline  
Old 16-02-2009, 09:17 PM   #11 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Default 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..
Pragadheesh is offline  
Old 17-02-2009, 08:33 AM   #12 (permalink)
Dreaming Future
 
kin.vachhani's Avatar
 
Join Date: May 2005
Location: \internet\home
Posts: 177
Default Re: factorial

Can i have a hint...
__________________
World without Computer is like a man without Brain.....
kin.vachhani is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:07 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2