0

I created this function and I need help turning this into a recursive function

#include "stdafx.h"
#include "stdio.h"

int fact (int n); // function prototype//

int main (void)
{
	int n,result;

	printf("please enter a positive integer");
	scanf("%d", &n);

	result = fact(n);

	printf("The answer is %d\n", result);

	return 0;
}

int fact(int n)
{
	int i, product=1;
		
	for(i=1; i<=n; i++) //algorithm
	{
		product *= i;
	}

	return (product);
}

Thanks for the guidance

4
Contributors
12
Replies
13
Views
8 Years
Discussion Span
Last Post by ahamed101
0

Perhaps you should make an effort to search on the form. There might be like tons of results on this issues. But here is the pesudo code.

<return type of int> function name FACT ( taking int as an argument )
{
   if n equals to zero
      return one;
   else
       return n multiple FACT ( n minus one );
}

NB: Learn to start using code tags!

ssharish

0

Is this correct for a recursive function I still have the following error (23) : error C2447: '{' : missing function header (old-style formal list?)

#include "stdafx.h"
#include "stdio.h"

int fact (int n); // function prototype//

int main (void)
{
	int n,result;

	printf("please enter a positive integer");
	scanf("%d", &n);
	result = fact(n);
	printf("The answer is %d\n", result);
	return 0;
}
	
   int return1 (int n);
   {
	   if (n<=1)
		   return1;
	   else
		   return(n*fact(n-1));
	}
0
int return1 (int n);

Why is that semicolon there??? Delete that! Syntax error

ssharish

When deleting the semicolon the following errors shows up error LNK2019: unresolved external symbol "int __cdecl fact(int)" (?fact@@YAHH@Z) referenced in function _main
C:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\lab 8.1\Debug\lab 8.1.exe : fatal error LNK1120: 1 unresolved externals

0
int return1 (int n);

Why is that semicolon there??? Delete that! Syntax error

ssharish

And why is it called return1??? It should be fact!!!

0

Well there are few thing which you need to consider. First of all the function name is not write. In you code you have declared a function prototype clearly, but why dosn;t that reflect in your function definiation.

Your function name is return1 which should be return1. And then within the function your should check for n <= 0 not 1 or n == 0 . And also if thats true you should return 1 not return1 . Give a space between return and 1.

ssharish

-1

Here it is...

#include "stdio.h"
#include "conio.h"
int fact (int n); // function prototype//

int main (void)
{
	int n,result;

	printf("please enter a positive integer");
	scanf("%d", &n);

	result = fact(n);

	printf("The answer is %d\n", result);

	getch();
	return 0;
}

int fact (int n)
{
	   if (n<=1)
		   return 1;
	   else
		   return(n*fact(n-1));
}
Comments
Point the mistake the OP did rather. Dont do his work!
0

how could I add something to the function so it calculates when and if n is negative

0

how could I add something to the function so it calculates when and if n is negative

Well, you got to be pretty careful on what your placing in the recursive function. The function is built to find the factorial for a given number. Tell me what are you trying to achieve.

Why do you want to check for n is negative??

if( n < 0 ) {   ...   }

This should check for the negative function. You could perhaps replace the statment in your fact function and see what output you get. I would guess not the right answer your looking for!

ssharish

0

Isn't negative factorial kind of wrong altogether?

YES it is :) Perhaps the OP should check the value of n, before sending the value to the fact function.

print "Value for n ?"
   read n

if( n >= 1 )
   print fact( n )
else
  Error

ssharish

0

how could I add something to the function so it calculates when and if n is negative

Yeah, negative factorial is wrong, why do you want that anyways?...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.