home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2860
Discussions: 188,380, Posts: 2,243,466, Members: 232,608
Old November 9th, 2002, 12:11 AM   Digg it!   #1 (permalink)
Member
 
techteen143's Avatar
 
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

techteen143 is offline   Reply With Quote
Old November 9th, 2002, 12:19 AM     #2 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,549
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.

jkrohn is offline   Reply With Quote
Old November 9th, 2002, 12:22 AM     #3 (permalink)
Member
 
techteen143's Avatar
 
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

techteen143 is offline   Reply With Quote
Old November 9th, 2002, 12:26 AM     #4 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,549
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
7! is 5040.....

I don't see the problem....

Jkrohn
jkrohn is offline   Reply With Quote
Old November 9th, 2002, 12:31 AM     #5 (permalink)
Member
 
techteen143's Avatar
 
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.
techteen143 is offline   Reply With Quote
Old November 9th, 2002, 12:33 AM     #6 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,549
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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
jkrohn is offline   Reply With Quote
Old November 9th, 2002, 12:37 AM     #7 (permalink)
Member
 
techteen143's Avatar
 
Join Date: Oct 2002
Location: Rochester, NY
Posts: 478
Thanx but what exactly does
nfact += i do?
techteen143 is offline   Reply With Quote
Old November 9th, 2002, 12:39 AM     #8 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,549
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
nfact += 1;

is "shorthand" for

nfact = nfact + i;

Jkrohn
jkrohn is offline   Reply With Quote
Old November 9th, 2002, 12:42 AM     #9 (permalink)
Member
 
techteen143's Avatar
 
Join Date: Oct 2002
Location: Rochester, NY
Posts: 478
Thanx havent gotten that far

-Techteen143
techteen143 is offline   Reply With Quote
Old November 9th, 2002, 01:45 AM     #10 (permalink)
Member
 
techteen143's Avatar
 
Join Date: Oct 2002
Location: Rochester, NY
Posts: 478
Another question

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.
techteen143 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2885)
The United States Debt (20)
Looks like Burris will get his Sena.. (8)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
Folderchat Weekday thread (439)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Help with an Ati Radeon HD 4850 512.. (25)
Recent Discussions
RCA 52Inch HDTV wont turn on (1)
Building a gaming computer advi.. (1)
Best digital camera for under 2.. (14)
Help with an Ati Radeon HD 4850.. (25)
Install Problem for Windows Def.. (0)
New Build ( Finally ) (1)
dual monitors wont boot (0)
Folderchat Weekday thread (439)
MSN Hotmail Down??? (7)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 08:50 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28