# n factorial while loop

• (2 Pages)
• 1
• 2

## 15 Replies - 17919 Views - Last Post: 09 November 2010 - 01:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=196066&amp;s=ed7f22807e3251db9015549bea15be33&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 busta5000

Reputation: -7
• Posts: 147
• Joined: 08-March 08

# n factorial while loop

Posted 21 October 2010 - 11:38 AM

Ok I already know the code but don't know how to write it in a proper manner I mean the doctor wants us to create a while loop cmd app that calculates:

Quote

The factorial of a nonnegative integer n is written n! (pronounced “n factorial”)
and is defined as follows:
n! = n×(n-1)×(n-2). . . × 1 (for values of n greater than 1)
and
n! = 1 (for n = 0 or n=1)
For example 5! = 5.4.3.2.1 = 120. Use the while statement to write a program that reads a nonnegative integer and computes and prints the factorial.

So what I simply did is analyse what I want & try to put it in the code!

so this is what I did so far

```  # include <iostream>
using namespace std;
int main()
{
int nfactorial;
int n;
int nloop;
int x;
int xloop;
x=1;
xloop=x+1;
while (nloop<n) {
nloop=(n-xloop)
cin>>n;
cout<<nfactorial = n * nloop;
}
return 0;
}
```

now I will explain what I did so that you know that I really put alot of thinking into this before being frustrated about it & try to get help from you guys!

_____________________________

what the professor wants is the output to be

Quote

5! = 5.4.3.2.1 = 120

the formula is

Quote

n! = n×(n-1)×(n-2). . . × 1

so what I did first is

determine the value of n by putting

```cout<<"please enter n:";
cin>>n;
```

now this is the hardest part for me I want to loop "n" n times!!!!
get it! so 5 factorial (5!)=5 x (5x1) x (5x2)x (5x3) x (5x4)x (5x5) x 1 = 120

so this is the hard part for me I couldn't understand what to loop in the meaning that I don't think that the code will understand the loop I did
```while (nloop<n) {
nloop=(n-xloop)
cin>>n;
cout<<nfactorial = n * nloop;
}
```

OUTPUT:
```1>------ Rebuild All started: Project: TEST, Configuration: Debug Win32 ------
1>Build started 10/21/2010 10:22:51 PM.
1>_PrepareForClean:
1>  Deleting file "Debug\TEST.lastbuildstate".
1>InitializeBuildStatus:
1>  Touching "Debug\TEST.unsuccessfulbuild".
1>ClCompile:
1>  TEST.cpp
1>c:\users\imac\documents\visual studio 2010\projects\test\test\test.cpp(16): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(66): could be 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(std::basic_ostream<_Elem,_Traits> &&)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\ostream(604): or       'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, int)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.88
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

```

So can you please help me modify the code but please teach me I don't want you guys to solve it I want to understand because this assignment will help me with understanding my studies.
I did search but most are irrelevant to the code I'm using.

Is This A Good Question/Topic? 0

## Replies To: n factorial while loop

### #2 jimblumberg

Reputation: 4647
• Posts: 14,572
• Joined: 25-December 09

## Re: n factorial while loop

Posted 21 October 2010 - 12:37 PM

```    while (nloop<n) {
}

```

What is the value of nloop and n at this point?

The error message is complaining about line 16 in test.cpp. Which is:

```cout<<nfactorial = n * nloop;

```

You should not try to assign a value in a cout statement. So your code should be:

```
nfactorial = n * nloop;
cout << nfactoial;

```

This will not fix all your logic/placement issues, but give it a try and see if you can clear up some of the other issues.

Jim

### #3 busta5000

Reputation: -7
• Posts: 147
• Joined: 08-March 08

## Re: n factorial while loop

Posted 21 October 2010 - 01:30 PM

jimblumberg, on 21 October 2010 - 11:37 AM, said:

```    while (nloop<n) {
}

```

What is the value of nloop and n at this point?

The error message is complaining about line 16 in test.cpp. Which is:

```cout<<nfactorial = n * nloop;

```

You should not try to assign a value in a cout statement. So your code should be:

```
nfactorial = n * nloop;
cout << nfactoial;

```

This will not fix all your logic/placement issues, but give it a try and see if you can clear up some of the other issues.

Jim

thanks it worked but got a weird error I don't understand it for this line

```cout<<"please enter n:";
```

this is the error:
```1>c:\users\imac\documents\visual studio 2010\projects\test\test\test.cpp(14): error C2146: syntax error : missing ';' before identifier 'cout'
```

any ideas!!!

for ur questions:

nloop is the loop of (n- k)
where k = n but times for example
n=5 then (5-1) * (5-2) * (5-3)* (5-4)* (5-5) x 1
n is any value I input.

This post has been edited by busta5000: 21 October 2010 - 01:34 PM

• Saucy!

Reputation: 6207
• Posts: 23,953
• Joined: 23-August 08

## Re: n factorial while loop

Posted 21 October 2010 - 01:49 PM

OK, thinking cap time.

So, you've got this error:
```syntax error : missing ';' before identifier 'cout'
```

which is reported for this line:
```cout<<"please enter n:";
```

What is the first word on this line, and what does that tell you about where you should look for a missing ;?

### #5 jimblumberg

Reputation: 4647
• Posts: 14,572
• Joined: 25-December 09

## Re: n factorial while loop

Posted 21 October 2010 - 01:52 PM

What I meant by:

Quote

What is the value of nloop and n at this point?

is what is the initial value of nloop and n when entering the loop?

For your new error check for a missing ';' on the lines before line 14.

Jim

This post has been edited by jimblumberg: 21 October 2010 - 01:53 PM

### #6 busta5000

Reputation: -7
• Posts: 147
• Joined: 08-March 08

## Re: n factorial while loop

Posted 22 October 2010 - 12:15 AM

jimblumberg, on 21 October 2010 - 12:52 PM, said:

What I meant by:

Quote

What is the value of nloop and n at this point?

is what is the initial value of nloop and n when entering the loop?

For your new error check for a missing ';' on the lines before line 14.

Jim

it apears I forgot ; in this line

```nloop=(n-xloop)
```

now its fixed & successful but I think the code is wrong?

I am surprised that I need to explain this further because its simple but the code is tricky so I'm having a hard time.

what I want to do is

4! = 4×(4-1)×(4-2)×(4-3)×(4-4) × 1

so do u understand what I want?

this is what I changed so far

```  # include <iostream>
using namespace std;
int main()
{
int nfactorial;
int n;
int nloop;
int x;
int xloop;
cin>>n;
x=1;
xloop=x+1;
cin>>xloop;
nloop=(n-xloop);
cin>>nloop;
while (xloop<=n) {
nloop=(n-xloop);
nfactorial = n * nloop;
cout<<nfactorial;
}
return 0;
}
```

It is successful but it doesn't give the result I want when the value of n lets say 4 nothing happens & when I type a random number it loops 8 infinity!

try to understand that what I want is that the value 4 should have 4 brackets & value 5 should have 5 brackets that is the loop

4! = 4×(4-1)×(4-2)×(4-3)×(4-4) × 1
5! = 5×(5-1)×(5-2)×(5-3)×(5-4)×(5-5) × 1

what I did is put the value of

Quote

n= any number I input
x= is the value that will change n times x =1
xloop= the loop of x n times n=4 x = 1 2 3 4
nloop= the loop of (n-xloop) n=4 (4-1) (4-2) (4-3) (4-4)
nfactorial= the hole forumla nfactorial=n*nloop

I think the code is missing lots of codes what did I miss????

### #7 busta5000

Reputation: -7
• Posts: 147
• Joined: 08-March 08

## Re: n factorial while loop

Posted 22 October 2010 - 04:44 AM

OK this is the CODE SIMPLIFIED thanks to a couple of my friends

```#include <iostream>
using namespace std;

int main () {
int num;
int fac = 1;
cout << "Please enter a number: ";
cin >> num;
while (num>0) {
fac = fac * num;
num--;
}
cout << "The Factorial is:" << fac << endl;
return 0;
}

```

but the problem is that when I type any number lets say 4 it gives me

the result & not the process

Quote

THE FACTORIAL IS:24

what I want it to be like is

Quote

4!=4x3x2x1x1=24

I tried to put fact"!" it doesn't work since there can't be "" without cout & can't be = with cout!

so what should I do?

### #8 busta5000

Reputation: -7
• Posts: 147
• Joined: 08-March 08

## Re: n factorial while loop

Posted 22 October 2010 - 05:12 AM

OK I did it thanks to (SILEX)

```#include <iostream>
using namespace std;

int main () {
int num;
int fac = 1;
cout << "Please enter a number: ";
cin >> num;
cout << endl << num << "! = ";
while (num>0)
{
fac = fac * num;
cout << num;
cout << ".";
--num;
}
cout << " = " << fac << endl;
return 0;
}

```

### #9 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: n factorial while loop

Posted 22 October 2010 - 05:19 AM

busta5000, on 22 October 2010 - 08:44 PM, said:

I tried to put fact"!" it doesn't work since there can't be "" without cout & can't be = with cout!

What in the name of Bob is that sentence above trying to say?
It is just gibberish.

busta5000, on 22 October 2010 - 09:12 PM, said:

OK I did it thanks to (SILEX)

Really?
Did you (or rather did SILEX)?
So you are saying that the output of that code is the following are you?
4!=4x3x2x1x1=24

Because I can promise you it absolutely does not produce that output.

This post has been edited by janotte: 22 October 2010 - 05:26 AM

### #10 busta5000

Reputation: -7
• Posts: 147
• Joined: 08-March 08

## Re: n factorial while loop

Posted 22 October 2010 - 06:40 AM

janotte, on 22 October 2010 - 04:19 AM, said:

busta5000, on 22 October 2010 - 08:44 PM, said:

I tried to put fact"!" it doesn't work since there can't be "" without cout & can't be = with cout!

What in the name of Bob is that sentence above trying to say?
It is just gibberish.

busta5000, on 22 October 2010 - 09:12 PM, said:

OK I did it thanks to (SILEX)

Really?
Did you (or rather did SILEX)?
So you are saying that the output of that code is the following are you?
4!=4x3x2x1x1=24

Because I can promise you it absolutely does not produce that output.

actually janotte I promise you that it works

do you want a snapshot to prove so

I also did %95 of the code the only thing I didn't know how to do was 3 lines

``` cout << endl << n << "! = ";
cout << ".";
--n;
```

This post has been edited by busta5000: 22 October 2010 - 06:44 AM

### #11 CTphpnwb

• D.I.C Lover

Reputation: 3325
• Posts: 11,775
• Joined: 08-August 08

## Re: n factorial while loop

Posted 22 October 2010 - 07:21 AM

5.4.3.2.1.1. = 120 is not the same as 5x4x3x2x1 = 120 or 5x4x3x2x1x1 = 120, and not because you've switched '.' for 'x'. The problem is that you've got one too many multiplication symbols. By the way, there's no need to loop all the way to one. You could do:
```while (num>1)
```

### #12 busta5000

Reputation: -7
• Posts: 147
• Joined: 08-March 08

## Re: n factorial while loop

Posted 22 October 2010 - 10:30 AM

CTphpnwb, on 22 October 2010 - 06:21 AM, said:

5.4.3.2.1.1. = 120 is not the same as 5x4x3x2x1 = 120 or 5x4x3x2x1x1 = 120, and not because you've switched '.' for 'x'. The problem is that you've got one too many multiplication symbols. By the way, there's no need to loop all the way to one. You could do:
```while (num>1)
```

actually I need the "." or "x" if you read the question I posted above you would understand.

### #13 CTphpnwb

• D.I.C Lover

Reputation: 3325
• Posts: 11,775
• Joined: 08-August 08

## Re: n factorial while loop

Posted 22 October 2010 - 07:30 PM

5x4x3x2x1 != 5x4x3x2x1x because the last 'x' makes it nonsensical.

### #14 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: n factorial while loop

Posted 23 October 2010 - 02:03 AM

busta5000, on 22 October 2010 - 10:40 PM, said:

actually janotte I promise you that it works

Well that just tells us that your promise is not worth anything at all.

As has been pointed out to you that code is badly broken.

I have replaced "." with "x" just to make the flaw already pointed out to you a bit clearer.

Here is the output of SILEX's code.
```Please enter a number: 4

4! = 4x3x2x1x = 24

```

What's that "x" on the right of 1 doing?
Adding decoration? Or just proving that SILEX and you did not test this code carefully enough.

Just as has been pointed out to you already this code is a fail.
So how about not throwing promises around when it is so easy to prove that your promise is an empty one.
I hope a valuable life lesson has been learned.

### #15 busta5000

Reputation: -7
• Posts: 147
• Joined: 08-March 08

## Re: n factorial while loop

Posted 09 November 2010 - 08:47 AM

janotte, on 23 October 2010 - 01:03 AM, said:

busta5000, on 22 October 2010 - 10:40 PM, said:

actually janotte I promise you that it works

Well that just tells us that your promise is not worth anything at all.

As has been pointed out to you that code is badly broken.

I have replaced "." with "x" just to make the flaw already pointed out to you a bit clearer.

Here is the output of SILEX's code.
```Please enter a number: 4

4! = 4x3x2x1x = 24

```

What's that "x" on the right of 1 doing?
Adding decoration? Or just proving that SILEX and you did not test this code carefully enough.

Just as has been pointed out to you already this code is a fail.
So how about not throwing promises around when it is so easy to prove that your promise is an empty one.
I hope a valuable life lesson has been learned.

sorry it took me long to reply but yea the code is kinda broken by adding "X" or "." but at least it doesn't change the result but I do want to learn how to remove that last "."?

I thought what you meant by code is broken as in it doesn't give the result! but it the code succeeded & it run smoothly & it gave me the correct results So only thing added was just a symbol so the code is not broken because if it is broken then the code must fail I'm just saying.

So can you please explain how to remove that last "." because I tried really I tried but I didn't succeed.