help

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

help

Postby zhenk » Tue Dec 02, 2003 4:11 pm

this is not hw but classwork... i wanna get a headstart since im not that great with this c++ :cry:

i need to make a small program:

A) write a program that reads a positive integer and displays its factorial. the program output should look similar to:

enter an interger: 5
5! = 120

B) 0! is identified as 1 , but all negative factorial are considered to be undefined. modify the factorial program to ask the user for an integer until a positive integer or 0 is entered. the output should look similar to:\

enter an integer: -5
-5! is undefined
enter an integer 0
0! = 1

i think this program needs to use loops and im not that certain on how to use them because we just learned it and i dont really understand much but i do know a little. i tried to start it.... here is my code

Code: Select all
#include <iostream.h>

int main()

{
   int num, fact;

   cout<<endl<<"Enter an integer";
   cin>>num;

   cout<<endl<<num<<"!="<<fact;

   for(i=0; i<=n; i--)
}
zhenk
 
Posts: 13
Joined: Sun Nov 02, 2003 3:10 pm

Postby WaltP » Tue Dec 02, 2003 6:22 pm

What is fact? I assume the factorial. Start it at 1.
You have an empty for() loop defined -- what do you think would need to go into that loop?

Use cout's within the loop to see what values are being generated as the loop ... loops
There are only 10 types of people in the world -- those that use binary, and those that don't
WaltP
 
Posts: 1187
Joined: Thu Oct 16, 2003 11:24 am

Postby HARiS_P » Tue Dec 02, 2003 9:47 pm

Well if you know how to get a factorial down on paper, then it shouldn't be hard.

lets say u use 5!. That is 5!=1*2*3*4*5=120.

so you can use a loop to go from 1 to the number you want to do a factorial of. In this example 5.

and in there you can solve.
10 SIN
20 GOTO HELL

HPSoft
User avatar
HARiS_P
 
Posts: 226
Joined: Tue Sep 23, 2003 10:01 pm
Location: chicago, il

Postby mfrank410 » Fri Dec 05, 2003 2:44 pm

Try this...

Code: Select all
num = 5;
iFact = 0;

for ( int i = 0; i < num; i++ )
{
     iFact = (num - i) * (num -(i+1));
}


that should do it...I dont have a compiler, but basically thats one way to do it.
Mike Frank
Co-Owner / Programmer
Frank & Mundula Consulting
http://www.frankmundulaconsulting.com
mfrank@frankmundulaconsulting.com
mfrank410
 
Posts: 50
Joined: Fri Oct 24, 2003 3:35 pm
Location: Ontario, Canada


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 4 guests