Factorials (C++)

coding without if or while loops.

Page 1 of 1

3 Replies - 2667 Views - Last Post: 08 January 2011 - 06:25 PM Rate Topic: -----

#1 chadihuoma   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 19-June 08

Factorials (C++)

Posted 08 January 2011 - 06:05 PM

Greetings, how do you write a code for computing factorials without using if and while statements, is it possible?. The first code i have (my code), part(a.) is for computing 5 factorial; however, this seems good for just a small number, but not so good for large numbers.

Part (b, & c) i obtained from my book and online, but they have if and while statements. The chapter i got this problem from (ch 3.) deals with (Numeric Types, Expressions, and Output) in which i have yet to cover if and while statements. This is where my problem is, how can i code something, when i haven't learned the constructs of how the answer is given. Thanks & God bless.

A.) // my code
// factorial for value 5

int n = 5;
int result = 1; 

result = n * (n-1) * (n-2) * (n-3) * (n -4);

// this seems ok, but if i had to do for a larger number like 18, 
// it's annoying to think that i would have to type:
//  result = n *(n-1) *.....(n-17).




//******************************************************************
B.) // code from book from (ch. 7, Functions)

int Factorial(/*in*/ int n)

// This function computes n!

//Precondition: n>= 0 && n! <= INT_MAX
//Postcondition: return value is n!

{
	int result;   // Holds partial products

	result = 1;
	while (n > 0)
	{
		result = result * n;
		n--;
	}
	return result;
}



//*************************************************************

C.) code obtained online from (cplusplus.com)
long factorial (long a)
{
  if (a > 1)
   return (a * factorial (a-1));
  else
   return (1);
}




Is This A Good Question/Topic? 0
  • +

Replies To: Factorials (C++)

#2 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Factorials (C++)

Posted 08 January 2011 - 06:19 PM

This is where my problem is, how can i code something, when i haven't learned the constructs of how the answer is given. 



You need to do independent research to solve your problems. You are not a slave to your book, read other information from other sources to help you understand and solve your problems :)

This post has been edited by ImaSexy: 08 January 2011 - 06:20 PM

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Factorials (C++)

Posted 08 January 2011 - 06:20 PM

You could use a for loop:
#include <iostream>

using namespace std;
int factorial( int N)
{
	int factorial = 1;
		for (int I = N; I > 1; I--) {
			factorial *= I;
		}
	return factorial;
}

int main (int argc, char * const argv[]) {
	cout << factorial(6);
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 lepinat0r   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 21-December 10

Re: Factorials (C++)

Posted 08 January 2011 - 06:25 PM

I doubt you have covered for loops then if you have not covered if statements..

Im going to try to figure it out without a for loop though :D


edit: Well CTphpnwn beat me to the code, but to piggyback off:

Quote

You need to do independent research to solve your problems. You are not a slave to your book, read other information from other sources to help you understand and solve your problems


This ^^^^

Seriously, my instructors, for a lack of better words have been "not much help." Mine, so far, have only taught me how they learned and expect you to do research on your own.

This post has been edited by lepinat0r: 08 January 2011 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1