November 9th, 2002, 12:11 AM
|
#1 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
| Another stupid C++ question
Alright how can I make this program add the "count" numbers individually to get the factorial of the integer? Obviously this does not work. I am pretty stupid.... Code: // purpose: Calculate and print product of the integers 1 through n
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
int n, nfact;
cout << "This program will calculate the factorial of a positive integer. ";
cout << "Input Integer" << endl;
cin >> n;
for(int count = 1; count <= n; count ++)
{
nfact = count + count;
}
cout << "The value of " << n << "! is: " << nfact << endl;
system("pause");
return 0;
} Thanx |
| |
November 9th, 2002, 12:19 AM
|
#2 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
|
Try this Code: #include <iostream.h>
#include <stdlib.h>
int main(void)
{
int n, nfact=1;
cout << "This program will calculate the factorial of a positive integer. ";
cout << "Input Integer" << endl;
cin >> n;
for(int count = 1; count <= n; count ++)
{
nfact = nfact * count;
}
cout << "The value of " << n << "! is: " << nfact << endl;
system("pause");
return 0;
} Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
November 9th, 2002, 12:22 AM
|
#3 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
Didn't work when I put in 7 I get 5040 not 28. My brain is off tonight...
Thanx tho |
| |
November 9th, 2002, 12:26 AM
|
#4 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
|
7! is 5040.....
I don't see the problem....
Jkrohn |
| |
November 9th, 2002, 12:31 AM
|
#5 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
supposed to be 1+2+3+4+5+6+7 or is that somethin else? That is wha I need... 7 is supposed to return 28...
Last edited by techteen143 : November 9th, 2002 at 12:35 AM.
|
| |
November 9th, 2002, 12:33 AM
|
#6 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
|
n! = 1*2*3*4......*n
If you are looking for the sum of consecutive integers up to n then use this code. Code: #include <iostream.h>
#include <stdlib.h>
int main(void)
{
int n, nfact=0;
cout << "This program will calculate the sum of consectutive integers from 1 to n. ";
cout << "Input Integer" << endl;
cin >> n;
for(int i=1; i <= n; i++)
{
nfact += i;
}
cout << "The value of the consecutive integers is"<<nfact;
system("pause");
return 0;
} Jkrohn |
| |
November 9th, 2002, 12:37 AM
|
#7 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
Thanx but what exactly does nfact += i do? |
| |
November 9th, 2002, 12:39 AM
|
#8 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,549
|
nfact += 1;
is "shorthand" for
nfact = nfact + i;
Jkrohn |
| |
November 9th, 2002, 12:42 AM
|
#9 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
Thanx havent gotten that far
-Techteen143 |
| |
November 9th, 2002, 01:45 AM
|
#10 (permalink)
| | Member
Join Date: Oct 2002 Location: Rochester, NY
Posts: 478
|
Got another question: What can I put here to make this program work? Code: // purpose: Price value table
#include <iostream.h>
#include <stdlib.h>
int main(void)
{
cout.precision(2);
cout.setf(ios::showpoint);
cout.setf(ios::fixed,ios::floatfield);
int TotalNumber;
double UnitPrice;
cout << "How many units do you want to buy?" << endl;
cin >> TotalNumber;
cout << "What is the unit price?" << endl;
cin >> UnitPrice;
cout << "Number of Units\t Total Price" << endl;
cout << "===============\t ===========" << endl;
for(int counter = 1; counter <= TotalNumber; counter ++)
{
cout << "\t" << counter << "\t $" << UnitPrice << endl;
UnitPrice = ;
}
system("pause");
return 0;
} Code: Ex.
input 5 & 1.50
# of units total price
1 1.50
2 3.00
3 4.50
etc... Thanx
Last edited by techteen143 : November 9th, 2002 at 01:48 AM.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |