468,491 Members | 1,962 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

logic of factorial

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

Sep 24 '06 #1
8 10964
compile time error? which compiler are you using?
salman wrote:
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;
}
Sep 24 '06 #2
salman wrote:
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 <iostream>
# 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: ";
cin>>n;
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
Sep 24 '06 #3
On 23 Sep 2006 19:39:27 -0700 in comp.lang.c++, "salman"
<ad***********@gmail.comwrote,
>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++.

Sep 24 '06 #4
In article <ef**********@murdoch.acc.Virginia.EDU>, jk********@gmx.net
says...

[ ... ]
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;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 24 '06 #5

Jerry Coffin wrote:
In article <ef**********@murdoch.acc.Virginia.EDU>, jk********@gmx.net
says...

[ ... ]
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%20rights%20and%20wrongs
K

Sep 24 '06 #6
Jerry Coffin wrote:
In article <ef**********@murdoch.acc.Virginia.EDU>, jk********@gmx.net
says...

[ ... ]
>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
Sep 24 '06 #7
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; ...

--

Frederick Gotham
Sep 24 '06 #8
In article <ef**********@murdoch.acc.Virginia.EDU>, jk********@gmx.net
says...

[ ... ]
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...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by patrick_woflian | last post: by
35 posts views Thread by aNt17017 | last post: by
59 posts views Thread by Umesh | last post: by
3 posts views Thread by Sugandh Jain | last post: by
1 post views Thread by doctor309 | last post: by
2 posts views Thread by becky808 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.