473,326 Members | 2,102 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,326 software developers and data experts.

factorial program

i want a program to do the task:
1!+2!+3!+4!+5!=
Mar 17 '08 #1
6 1056
sicarie
4,677 Expert Mod 4TB
There are a number of ways to go about this, have you created your algorithm for completing this yet?

Also, are you going to just be printing out the final answer of that exact equation, or do you want to be able to change the highest number that it can go up to? Because if it is the first, you can just figure it out by hand and write a print statement. The latter will require actually creating an algorithm to figure out factorials.
Mar 17 '08 #2
JosAH
11,448 Expert 8TB
For S(n) == 1! + 2! + 3! + n! you can easily find that S(n)= S(n-1)+ (n-1)!*n so
if you keep the last term apart from the grand result you can always easily
calculate the next term.

kind regards,

Jos
Mar 17 '08 #3
fual
28
i want a program to do the task:
1!+2!+3!+4!+5!=
You can do this very efficiently with a function object:
Expand|Select|Wrap|Line Numbers
  1. class sum_fac {
  2.   const long& operator( )( void ) {
  3.     // compute the new factorial
  4.  
  5.     // Increase the partial sum
  6.     return sum_;
  7.   }
  8.   private:
  9.     long _i;
  10.     long _fac;
  11.     long _sum;
  12. };
  13.  
See if you can add in the details.
Mar 17 '08 #4
Thanx for the suggestion...
I made it using nested loops....
Mar 21 '08 #5
Thanx for the suggestion...
I havent done classes yet....
I made it using nested loops....
Mar 21 '08 #6
Ganon11
3,652 Expert 2GB
Have you considered using an array to do this?

e.g. User says to sum the first 6 number's factorials.

You make an array of length 6. The first element (array[0]) is 1. (This represents 1!).

Then, for any element N, assuming the previous element has already been set, the value needed in array[N-1] is array[N-2] * (N-1).

To find the sum, add all the numbers in the array.

This is a much faster method of calculating factorials then either nested loops or a recursive function, which are the two most common methods I've seen for calculating factorials.
Mar 21 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Martin Jørgensen | last post by:
Hi, Consider (factorial.cpp): #include <iostream> using namespace std; double R=3.2; /* not used, but R is static because it is a global variable (file scope) */
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;
8
by: salman | last post by:
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: ";
1
by: Tin | last post by:
Dear Sir/Madam, I have one assignment , which need me to write a program to calculate the factorial of an integer, then press the button that would display the result in a label.3 & the...
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...
12
by: dynamo | last post by:
Hello everyone,i am a beginner in c++ and i am trying to write a program that outputs the factorial of a number in my own way,however i don't know why it's not working.Thanks for any help.This is the...
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...
3
by: Blue sky | last post by:
Hi,I am a new C++ learner.The follow prgram produces a wrong result,but I can't find the wrong.Can you help me?Thank you! #include<stdio.h> long factorial( long number); int main() { int...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.