Hello! This is my first post and I am a beginner in Java programming-so don't come down on me too hard :) I have a problem that I need to solve over the weekend and I was hoping some of you could help me out. The problem at hand is that I need to compute n! where n is an integer entered by the user. the catch is that I need to store the answer in an array (up to 50 places) and print back the array such like (12! = ) 479001600 and being able to omit all leading zeros from the printout. I know I need to divide by 10 and mod by 10 to get the remainder so I can store the value in the next element of the array, but I don't know how. Our teacher told us to go ahead and load the array with zeroes and initialize the last element (x.length - 1) to 1 because 0! and 1! are 1 so that made sense to me, and I believe I have the code correct to compute the factorial, I just need some ideas/help as to storing those values in the array and printing that array...so here goes

import java.util.*;
public class Prog3
{
    public static void main(String[] args)
        {
            double n;
            double factorial = 1;
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a non-negative integer and I will compute its factorial");
            n = input.nextDouble();
            while (n<0)
          {
              System.out.println("Please enter a NON-negative number!");
              n = input.nextDouble();
          }
          while (n >0)
          {
              factorial = factorial * n;
              n--;
           }
            int[] x = new int[50];
            for(int i = 0; i <x.length; i++)
                x[i] = 0;
            x[x.length - 1] = 1;

           for(int       
           
           
            }
    }

*EDIT* of course I wrote computer in the title instead of *compute* sorry!

create function;

void getFact(long N);  //assume already created

for(int i = 0; i < MAX; i++)
      Array[i] = getFact(i);

I truly do not understand what you just wrote. I'm sorry

void getFact(long N); //assume already created

I am confused where "void" came from and all the code you see in my first post is all that I have written

for(int i = 0; i < MAX; i++)
      Array[i] = getFact(i);

and the "MAX" where did that come from? I do understand the for loop (we just went over that last week)

and I don't understand where "Array" came from

and This problem is for my 100-level class--my teacher must be insane--none of us have prior programming knowledge and expects us to know arrays like the back of our hand

I truly do not understand what you just wrote. I'm sorry

void getFact(long N); //assume already created

I am confused where "void" came from and all the code you see in my first post is all that I have written

for(int i = 0; i < MAX; i++)
      Array[i] = getFact(i);

and the "MAX" where did that come from? I do understand the for loop (we just went over that last week)

and I don't understand where "Array" came from

and This problem is for my 100-level class--my teacher must be insane--none of us have prior programming knowledge and expects us to know arrays like the back of our hand

I'm sorry my mistake, it should have been int getFact(int N);

Max is just any number. I just put it there so, as an arbitrary value.
Its not meant to be a full code, just enough to give you a hint.

int getFact(int N)
{
  int Tot = 1;
  while(N > 0 )
  { 
      Tot *= N; 
      N--;
  }
  return Tot;
}

somewhere in your main :

final int MAX = 5;
int Array[MAX] = new int[MAX];
for(int i = 0; i < MAX; i++){
    Array[i] = getFact(i);

//Print out Array
}

Is that what you were looking for?

for a factorial program, its always best to create a recursive factorial method that returns the answer to the factorial.

here is a recursive method for a factorial:

public static long factorial(int n)
{
    if (n <= 1)
        return 1;
    else
        return n * factorial( n – 1 );
}

see if you can work with that ...maybe you can modify the code to store the answer in an array...

for a factorial program, its always best to create a recursive factorial method that returns the answer to the factorial..

Uhh, Nope. Where would you get that idea? Recursive functions are expensive. It should be only used when it simplifies a lot of complexity,
and has reasonable performance.

alright, so I have gotten the program to actually calculate the factorial of a number and it prints the correct value. but numerous times! I think we're getting closer

import java.util.*;
public class Prog3
{
    public static void main(String[] args)
        {
            int n;
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a non-negative integer and I will compute its factorial");
            n = input.nextInt();
            while (n<0)
          {
              System.out.println("Please enter a NON-negative number!");
              n = input.nextInt();
          }
        
           int [] Array = new int[50];
           for(int i = 0; i <Array.length; i++)
           {
               Array[i] = getFact(i);
               
               System.out.println(Array[n] + " " );
           }
        }
           public static int getFact (int N)
           {
               int Total = 1;
               while (N > 0)
               {
                   Total *= N;
                   N--;
                }
                return Total;
           }
        }

you're probably right, but i didn't think the person was creating the code for optimized results. anyhow, i just replied as a suggestion of how i would do it.

Hello MMD88
at begin define 0! in array at index 0 ; 0! = 1
set currentIndex to 0
currentIndex - indicates last calculated factorial in array

array[0] = 1;
currentIndex = 0;

public static int factorial(int k) {
            if (k < 0) {
                k = 0;
            }
            if (k > currentIndex) {
                //fill array from currIndex+1 to k
                for (int i = /*start condition*/ ; /*end condition*/ ;i++){ //TODO
                // example array[1] = array[0] *1;
                // example array[2] = array[1] * 2;
                // example array[3] = array[2] * 3;
                // example array[12] = array[11] * 12;

                    array[i + 1] = array[i] * (i + 1);
                }
                currentIndex = ... //TODO  refresh currentIndex
            }
            // for  k <= currentIndex  NOTHING TO DO (proper value inside array)
            return array[k];
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.