• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hi I am stuck. Please help me

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I need a program which should calculate the factorial of given number. (the number is entered by the user)
(the factorial of 4 for example is 4*3*2*1 answer is 24)
My question is "how do I calculate the factorial of a given number?"

Can you please help me? I have stick on this for some time.
(btw I am new to programming)

class Conpronum
{
public static void main(String[ ]args)
{
//Declare variables
int num1

//Read in the user number
System.out.println ("\nPlease enter your a whole number:");
num1 = EasyIn.getInt();


This is all I have done so far. I don't know how to tackle this problem.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say your number is N.
You must use a loop. Before the loop, set a variable "f" to 1, and another variable "n" to N. Then, each time through the loop, multiply f by n, then subtract 1 from n. When n gets to be 1, you're done, and you can exit the loop, and "f" holds the answer.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About the recursive method (beautifully simple but a little tough for a beginner to understand):
http://mindprod.com/jgloss/recursion.html
About the iterative method:
Algorithm
Get the number.
To begin, set the factorial of the number to be one.
(Create an int named "factorial")
While the number is greater than one
Set the factorial to be the factorial multiplied by the number.
Decrement the number.
Print out the factorial
(source: http://www.cs.tcd.ie/Jennifer.Foster/LecturesByTopic/DoWhile/)
 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx alot Ernest.
I will try and make these changes. But I new to programming so if I get stuck again trying to make these changes I will post here.
Thx
Macca
 
macca Mason
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx Elouise
I will try my best to follow this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic