logic of factorial

S

salman

this program is giving compile time error. so plse ge me the logic of
factorial


# include <iostream.h>
# include <math.h>
void main()
{
int f,sum=0,i,j,n;
cout<<"\nEnter Number: ";
cin>>n;
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
f=f*j;
sum=sum+f;
}
cout<<"Summation Of Factorial: "<<sum<<endl;
}
 
K

Kai-Uwe Bux

salman said:
this program is giving compile time error. so plse ge me the logic of
factorial


# include <iostream.h>

This header does not exist in standard C++, and never has.

#include said:
# include <math.h>

never used.
void main()

another error: main returns an int.

int main ()
{
int f,sum=0,i,j,n;
cout<<"\nEnter Number: ";

cout and friends are in namespace std.

std::cout <<"\nEnter Number: ";

std::cin >> n;
for(i=1;i<=n;i++)
{
f=1;
for(j=1;j<=i;j++)
f=f*j;
sum=sum+f;
}
cout<<"Summation Of Factorial: "<<sum<<endl;

std::cout <"Summation Of Factorial: "<<sum<<std::endl;


A note on style: the code is horrible. The code wants to be written more
like this:

# include <iostream>

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

int main() {
unsigned long sum = 0;
std::cout << "\nEnter Number: ";
unsigned long n;
std::cin>>n;
for( unsigned long i = 1; i <= n; ++i ) {
sum += factorial( i );
}
std::cout << "Summation Of Factorial: "
<< sum
<< '\n';
}

Note:

(a) I included only the header I need.
(b) I isolated computation of the factorial.
(c) I declared the running index within the for loop.
(d) I used '\n' instead of std::endl.
(e) I used prefix ++ instead of postfix ++.
(f) I put initialization of every variable very close to its declaration.

Those are clear improvements. The following changes could be contested:

(g) I used braces in the for loop even though its body is one statement.
(h) I changed the types to unsigned.


Best

Kai-Uwe Bux
 
D

David Harmon

On 23 Sep 2006 19:39:27 -0700 in comp.lang.c++, "salman"
this program is giving compile time error. so plse ge me the logic of
factorial

No fair asking about an error message without telling us what it
says!
# include <iostream.h>

There is no such header in standard C++.
 
J

Jerry Coffin

[ ... ]
unsigned long factorial ( unsigned long n ) {
unsigned long result = 1;
while ( n > 0 ) {
result *= n;
--n;
}
return ( result );
}

While I can see reasons for writing the code this way, for a simple loop
counting n1..n2, I think a for loop is more readable:

unsigned long factorial(unsigned long n) {
unsigned long result = 1;
for (unsigned long i=2; i<=n; ++i)
result *= i;
return result;
}
 
K

Kai-Uwe Bux

Jerry said:
[ ... ]
unsigned long factorial ( unsigned long n ) {
unsigned long result = 1;
while ( n > 0 ) {
result *= n;
--n;
}
return ( result );
}

While I can see reasons for writing the code this way, for a simple loop
counting n1..n2, I think a for loop is more readable:

unsigned long factorial(unsigned long n) {
unsigned long result = 1;
for (unsigned long i=2; i<=n; ++i)
result *= i;
return result;
}

You might be right. However, for loops in C++ were the very hardest thing
for me to learn: for more than 5 years I never touched a for loop and
expressed everything in while loops because, on the initial line of a for
loop, there is just too much stuff going on. It took me quite a while to
get my mind around this totally wicked, overly general syntax. For loops
were the last C++ feature that I could add to my vocabulary. Everything
else (including STL, try-throw-catch, templates, inheritance) was easy in
comparison. To this day, I find while loops easier to understand; but I
grew used to for loops for iterating over containers. I could write page
long rants about "for". But I guess, that's just me: so you are probably
right.


Best

Kai-Uwe Bux
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Jerry said:
[ ... ]
unsigned long factorial ( unsigned long n ) {
unsigned long result = 1;
while ( n > 0 ) {
result *= n;
--n;
}
return ( result );
}

While I can see reasons for writing the code this way, for a simple loop
counting n1..n2, I think a for loop is more readable:

unsigned long factorial(unsigned long n) {
unsigned long result = 1;
for (unsigned long i=2; i<=n; ++i)
result *= i;
return result;
}


To the OP,

Factorial can be written either as a loop (as the two above are
written) or recursively (having the function call itself). For an
introduction to recursion and reasons why both posters used a loop in
this case you can read:

http://www.kirit.com/Recursive rights and wrongs


K
 
F

Frederick Gotham

salman posted:
int f,sum=0,i,j,n;


Are you writing C89? C++ does not forbid the mixing of declarations and
statements in a function body; you can define an object exactly where you
want to start using it in a function. If you have a counter in a "for" loop,
it's handy to define it in the first "compartment":

for( T i = 0; ...
 
J

Jerry Coffin

[ ... ]
You might be right. However, for loops in C++ were the very hardest thing
for me to learn: for more than 5 years I never touched a for loop and
expressed everything in while loops because, on the initial line of a for
loop, there is just too much stuff going on. It took me quite a while to
get my mind around this totally wicked, overly general syntax. For loops
were the last C++ feature that I could add to my vocabulary. Everything
else (including STL, try-throw-catch, templates, inheritance) was easy in
comparison. To this day, I find while loops easier to understand; but I
grew used to for loops for iterating over containers. I could write page
long rants about "for". But I guess, that's just me: so you are probably
right.

I suppose I have the "benefit" of having previously used PL/I do loops,
which make C and C++ for loops look simple by comparison. PL/I tried to
have the generality of the C for loop, but also the conversational style
of something like Pascal. If memory serves, this would have been
perfectly legal:

y = 1;
do i = 1 to 10 by Y, 21 by -1 to i while x < limit;
y = 2;
/* ... */
end;

Since it's undoubtedly not obvious to people who've only dealt with sane
languages, with a loop like that, i would take the following values:

1 2 3 4 5 6 7 8 9 10 21 20 19 18 17 16 15 14 13 12 11 10

[assuming the 'x<limit' part didn't cause a premature exit].

I.e. the assignment to 'y' inside the loop didn't affect the value of
'y' used by the loop body in the increment, and the assignments to 'i'
in evaluating the loop didn't affect the value that was compared against
in the second count specification.

Of course that's all based on distant memories, so I probably got a
detail or two (especially in the syntax) a bit wrong, but I suspect the
general idea comes through in any case. Then again, rules in PL/I were
like rules in English -- every one of them had so many exceptions that
every real situation was really just a collection of special cases...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top