Howdy Folks!

I've been asked to write a program that reads in an interger N and calculates The factorial ...

I wrote this yesterday but it does not seem to work .. here's the code :

#include <iostream>

using namespace std;

int main()
{
	int i=0; 
	int s=1;
	int N;
	int factorial;

	cout << "Please Enter a number";
	cin >> N;

	while ( i <= N )
	{ factorial = (N-i)*s;
	  s = (N-i);
	  i=i+1;
	}

	return 0;
}

What is wrong with it ?

Howdy Folks!

I've been asked to write a program that reads in an interger N and calculates The factorial ...

I wrote this yesterday but it does not seem to work .. here's the code :

#include <iostream>

using namespace std;

int main()
{
	int i=0; 
	int s=1;
	int N;
	int factorial;

	cout << "Please Enter a number";
	cin >> N;

	while ( i <= N )
	{ factorial = (N-i)*s;
	  s = (N-i);
	  i=i+1;
	}

	return 0;
}

What is wrong with it ?

Well, what results do you get? Nothing! How do you know whether anything is right or wrong if you never display anything? Display factorial, run it for a variety of N values, and see what you get.

#include <iostream>

using namespace std;

int main()
{
    int i=0;
    int s=1;
    int N;
    int factorial=1;
   int sum=1;

    cout << "Please Enter a number";
    cin >> N;

    while(N>1)
   {
    factorial = N * (N-1);
      sum*=factorial;
      N-=2;

   }

   cout<<sum<<endl;

    return 0;
}

Edited 3 Years Ago by Dani: Formatting fixed

#include <iostream.h>
#include <conio.h>


int main()
{
    int i=0;
    int s=1;
    int N;
    int factorial=1;
   int sum=1;

    cout << "Please Enter a number";
    cin >> N;

    while(N>2)
   {
    factorial = N * (N-1);
      sum*=factorial;
      N-=2;

   }

   cout<<sum<<endl;

   getch();
    return 0;
}

Edited 3 Years Ago by Dani: Formatting fixed

Well, what results do you get? Nothing! How do you know whether anything is right or wrong if you never display anything? Display factorial, run it for a variety of N values, and see what you get.

On line 17, factorial's value is always getting overriden by its previous value, however here we have to preserve previous values.

Well, what results do you get? Nothing! How do you know whether anything is right or wrong if you never display anything? Display factorial, run it for a variety of N values, and see what you get.

Well, :-/ it displays nothing but the prompt and when I enter the number it displays the "press any key to continue" which ends it :S

Well, :-/ it displays nothing but the prompt and when I enter the number it displays the "press any key to continue" which ends it :S

Did you, by any miraculous chance, write a line that should display the output? Scan your code and if it is there, i will poop rainbows :P

ooooops!

Happens sometimes, doesn't it? :)
Be more careful next time.

EDIT: Did you place the output line inside the loop, or outside? That can make a difference.

Edited 7 Years Ago by pspwxp fan: n/a

well now I wrote the output line .. but hmm the result for N=5 is zero

Right. Now you can start debugging the actual program. My point was that you can't know whether your results are correct without displaying them. You should also stick some cout values INSIDE the loop so you can see what happens at each step. It'll help you debug.

I placed it outside the loop ..

The problem was in the way of calculating

while ( i <= N )	
             { factorial = (N-i)*s;	
                 s = (N-i);	
                   i=i+1;	}

I confused the factorial with the "s" which is the sum and the condition was not correct I shouldn't have put the "equal sign"

It should have been this way

while ( i < N )
	{ factorial = (N-i);
	  s*=(N-i);
	  i=i+1;
	}

Right. Now you can start debugging the actual program. My point was that you can't know whether your results are correct without displaying them. You should also stick some cout values INSIDE the loop so you can see what happens at each step. It'll help you debug.

what kind of cout values shall I place inside the loop ??

I placed it outside the loop ..

The problem was in the way of calculating

while ( i <= N )	
             { factorial = (N-i)*s;	
                 s = (N-i);	
                   i=i+1;	}

I confused the factorial with the "s" which is the sum and the condition was not correct I shouldn't have put the "equal sign"

It should have been this way

while ( i < N )
	{ factorial = (N-i);
	  s*=(N-i);
	  i=i+1;
	}

The factorial variable as you have it does nothing. You can delete that line. I'd actually name your "s" variable "factorial". It's more descriptive. There's no summation involved.

what kind of cout values shall I place inside the loop ??

Well, if you've already solved it, it's a moot point, but I was thinking you would display the factorial variable inside the loop and see if it was changing correctly each time through the loop. Something like this:

factorial = 1
factorial = 2
factorial = 6
factorial = 2[B]4[/B]

If you weren't getting something like that, it would be a clue that something was wrong.

Edited 7 Years Ago by VernonDozier: n/a

Well, if you've already solved it, it's a moot point, but I was thinking you would display the factorial variable inside the loop and see if it was changing correctly each time through the loop. Something like this:

factorial = 1
factorial = 2
factorial = 6
factorial = 2[B]4[/B]

If you weren't getting something like that, it would be a clue that something was wrong.

Aha ... I see

The factorial variable as you have it does nothing. You can delete that line. I'd actually name your "s" variable "factorial". It's more descriptive. There's no summation involved.

oh that's a good point, it actually was not doing a thing!

thanks for pointing out

ooooops!

i fix ur logical error, now its upto u to transform ur code with minor changes according to ur compiler/IDE.

This article has been dead for over six months. Start a new discussion instead.