0

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 ?

4
Contributors
18
Replies
19
Views
7 Years
Discussion Span
Last Post by warbird43
Featured Replies
  • [QUOTE=new programer;1017463]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 : [CODE] #include <iostream> using namespace std; int main() { int i=0; int s=1; … Read More

  • [QUOTE=new programer;1017574]I placed it outside the loop .. The problem was in the way of calculating [CODE]while ( i <= N ) { factorial = (N-i)*s; s = (N-i); i=i+1; }[/CODE] I confused the factorial with the "s" which is the sum and the condition was not correct I shouldn't … Read More

  • [QUOTE=new programer;1017583]what kind of cout values shall I place inside the loop ??[/QUOTE] 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 … Read More

1

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.

1
#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 by Dani: Formatting fixed

0
#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 by Dani: Formatting fixed

0

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.

0

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

0

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

1

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 by pspwxp fan: n/a

0

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.

-1

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;
	}
0

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 ??

1

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.

1

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 by VernonDozier: n/a

0

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

0

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

-1

ooooops!

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

This topic has been dead for over six months. 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.