473,387 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 11340
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
3
by: naeemulhaq | last post by:
Can anyone suggest a better solution to finding the digit frequencies in factorial of a number, like 3! = 6 (0) 0 (1) 0 (2) 0 (3) 0 (4) 0 (5) 0 (6) 1 ...
59
by: Umesh | last post by:
i wrote the following program to calculate factorial: #include<stdio.h> #include<iostream.h> void main() { int i,n; long int p=1; // or long double p=1; for exponential result which I don't...
3
by: Sugandh Jain | last post by:
Hi. How to write a function that will return me the factorial (say in a string) for the any positive integer it takes? When we find a factorial of even say 2000 or a higher number, it will be...
0
by: Killer42 | last post by:
Here's a very simple function for VB (written in VB6) to calculate the factorial (!) of a number. Note that it is limited by the magnitude of the value which can be stored in the Long data type. (In...
0
kadghar
by: kadghar | last post by:
Hi, I saw that Killer posted a simple Factorial Function that allows you to calculate up to 13!, well, you can use this for bigger numbers by changing the variable type. Why is this? You can...
1
by: doctor309 | last post by:
hello friends iam not able to get logic behind solving the question mentioned below,,,,i tried a lot using various books.....online tutorials but at the end just lost it...... you ppl are kindly...
2
by: becky808 | last post by:
Hi, I'm trying to write a program to calculate n factorial but it won't compile. Can anyone tell me what I'm doing wrong? #include <iostream> #include <cmath> using namespace std; int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.