473,804 Members | 3,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<"Summatio n Of Factorial: "<<sum<<end l;
}

Sep 24 '06 #1
8 11362
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<<"Summatio n Of Factorial: "<<sum<<end l;
}
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<<"Summatio n Of Factorial: "<<sum<<end l;
std::cout <"Summation Of Factorial: "<<sum<<std::en dl;
}

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**********@m urdoch.acc.Virg inia.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(unsig ned 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**********@m urdoch.acc.Virg inia.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(unsig ned 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**********@m urdoch.acc.Virg inia.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(unsig ned 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 "compartmen t":

for( T i = 0; ...

--

Frederick Gotham
Sep 24 '06 #8
In article <ef**********@m urdoch.acc.Virg inia.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
9953
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 the following calculation: z = x! / (x- y)! The following code is my attempt and i was hoping for a point in the right direction as too where i am going wrong.. cheers
35
3988
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
2696
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 (7) 0 (8) 0 (9) 0 8! = 40320 (0) 2 (1) 0 (2) 1 (3) 1 (4) 1 (5) 0 (6) 0 (7) 0 (8) 0 (9) 0
59
55771
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 want. cout<<"Enter No. ";
3
8019
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 very big to be accomodated in int or long datatype. Regards, Sugandh
0
25675
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 fact, I believe it overflows at 13!) Public Function Factorial(ByVal bNum As Byte) As Long Dim I As Long If bNum <= 0 Then Exit Function Factorial = 1 For I = 1 To bNum Factorial = Factorial * I Next End Function
0
12318
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 use factorial on integers (that should be the input), the output could be a string or a double, anyway you will alwas have the factorial. The only restriction is in the input. Yes, i mean this function works fine for values up to 170! or something...
1
3288
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 requested to provide me help in order to save my interest in programming..... ok the question is: Write a program in c++ to find the factorial of a given number.For doing this create a class factorial in which define two methods get_Number(int)...
2
4892
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 main(){
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10594
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.