Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2016
    Posts
    3
    Rep Power
    0

    Default Java Factorial Program

    Hello,

    I've posed this question in a couple other places as well, but to no avail, so here I am....



    So I'm new to Java (and this forum), but was hoping I could get some help from you fine folks!

    Anyway, I'm supposed to write a Java program that will calculate the factorial of a given number.

    This program has to do the following:
    1. prompts the user for a number
    2. receives user input from the command line and calculates the factorial
    3. print outs the factorial on the command line


    I've been able to find templates for it, but wanted to see what the final product would look like?


    Thanks in advance!!
    Last edited by TheWrathOfKahn; 10-17-2016 at 01:32 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java Factorial Program

    wanted to see what the final product would look like
    If you have a template for the program, go ahead and start trying to write it. If you have problems, post the code in code tags and explain what problems you are having.

    Also posted at: https://coderanch.com/t/671586/java/...rogram#3139605
    Last edited by Norm; 10-17-2016 at 01:28 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Oct 2016
    Posts
    3
    Rep Power
    0

    Default Re: Java Factorial Program

    Well, I'm juuust getting started in Java, so I'm not entirely sure what I'm doing haha

    Here's the template I've got....


    Java Code:
    import java.util.Scanner;
    
    class Factorial
    {
    public static void main(String args[])
    {
    // initialize variables n, c, fact and set all to 1
    
    // print "Enter an integer to calculate its factorial"
    
    Scanner in = new Scanner(System.in);
    
    n = in.nextInt();
    
    if ( n < 0 )
    // print "Number should be non-negative."
    else
    {
    // use a for loop with c = 1 ; c <= n ; c++
    fact = fact*c;
    
    // print "Factorial of " n "is = " fact 
    }
    }
    }
    Last edited by TheWrathOfKahn; 10-17-2016 at 01:56 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java Factorial Program

    What happens when you compile and execute the code?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Oct 2016
    Posts
    3
    Rep Power
    0

    Default Re: Java Factorial Program

    Anybody?? Heeeeeellllllppppp!

    This is where I'm at as of now...

    Java Code:
    public static void main(String[] args) {
        int num;
        int fact=1;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        num = input.nextInt();
        for (int i=2;i<=num; i++){
            fact=fact*i;
        }
    
        System.out.println("Factorial: "+fact);
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java Factorial Program

    Ok, what happens when you try to compile and execute it?

    I'm done for tonight. I'll be back tomorrow.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. factorial java thread problem
    By alwan2009 in forum New To Java
    Replies: 14
    Last Post: 03-18-2012, 04:23 PM
  2. Factorial in java
    By spidey32 in forum New To Java
    Replies: 18
    Last Post: 04-09-2011, 01:04 PM
  3. factorial sum in java
    By java157 in forum New To Java
    Replies: 9
    Last Post: 03-17-2011, 10:07 AM
  4. Factorial console program
    By er1c550n20 in forum New To Java
    Replies: 6
    Last Post: 04-13-2010, 02:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •