My assignment is to Write an application that reads a non-negative integer and prints the factorial of the integer using the for loop. I am hopelessly lost and don't even know where to begin with this. Does anyone have any advise to get me started? Thank you

Break the project down into steps.
One step is to read in a non negative integer.
Write a java program to do that. Compile it and execute it. Come back when you have problems. Post the full text of the error messages and the program.

Now look up the formula for factorial.
Write down the computation steps to compute the factorial for 5.

We'll continue with the program design after you get the above done.

/* “PositiveInt.java” receives an integer greater than 0. In this program Exceptions are catched accordingly */
import javax.swing.*;
public class PositiveInt{
	static class NonNegativeException extends Exception{
	public NonNegativeException(String msg){
		super(msg);
	}	
	}
	static void show(int x)throws NonNegativeException {
		if (x<0)
		throw new NonNegativeException("Negative integer is not valid.");
	}
    public static void main(String args[]){
    	int n=0;
	while(true){
		try{
		String s=JOptionPane.showInputDialog(null,"Input a positive integer:");
    	n = Integer.parseInt(s);
    	show(n);
    	}catch(NumberFormatException e){
    	System.out.println();
    	JOptionPane.showMessageDialog( null, "Your input is mismatched. Try again.", "Warning ",JOptionPane.WARNING_MESSAGE);
    	continue;
    	}catch(NonNegativeException e){
    	JOptionPane.showMessageDialog( null, e.getMessage(), "Warning ",JOptionPane.WARNING_MESSAGE);
    	continue;	
    	}
    	 break;    
    }
    JOptionPane.showMessageDialog(null,"Your input: "+n,"Welcome to DANIWEB", JOptionPane.INFORMATION_MESSAGE);
    }
}
/* Operation on DOS window: "PositiveInt1" receives a positive integer only.  */
import java.util.*;
public class PositiveInt1{
	
    public static void main(String args[]){
    	int n=0;
    	Scanner in = new Scanner(System.in);
    	
   	while(true){
	try{
System.out.println("Input a non-negative integer:");
	n = in.nextInt();
   	}catch(InputMismatchException e){
	System.out.println("Your input is mismatched.");
	in.next();
 	continue;
  	}
  	if (n<0){
  	System.out.println("You negative value is not valid.");
  	continue;
  	}
       	break;
    }
	
    System.out.printf("Your input is %d\n", n);
	}
}

In the following method, why the factorial goes wrong when n>12 ? */

static int factorial(int n){
		int fac=1;
		if ((n<0) || (n>12))
		return -1;
		for (int i=2; i<=n;i++)
		fac *=i;
		return fac;
	}

Please start your own thread if you have questions.
Don't hijack someone else's thread.

Thank you for your advise.

So I've made a little progress, I'm reading the nonnegative integer, displaying the factorial, but I'm trying to get the 5!=5*4*3*2*1=120 to display, it is only giving me the product.

import java.util.Scanner;

/**
 *Odd and even, if all input numbers are even 1 is displayed or if either number is odd 0 is displayed.
 * Cpsc118 Computer programming 1
 * INSTRUCTOR   John Roberts
 * ASSIGNMENT    Assignment #4, question #1
 * @author       Dylan Godfrey, 100120344
 * @version      July 24 2010
 */


public class a4q1
{
     public static void main(String args[])
     {
           Scanner input =new Scanner (System.in);
           int num,factorial=1;
          
          
           do
           {
          System.out.print("Please enter a number greater than or equal to 0.\n>>");
           num =input.nextInt();
           }
           while (num <0);
          
           for (int i =num; i > 0; i--)
          factorial*=i;
          
          System.out.println(num + "! = " +factorial);       
    }
}

Please post the input and output for the program. What does it display?

If enter 5 for example it displays 5!=120 I need to display 5!=5*4*3*2*1=120

dylgod, your line 26 is correct.

You need to add some print() statements to put out the extra parts of the output.
print() will output to the same line.
Or you could build the String to output bit by bit and only use one call to println() to output the String.

dylgod, Sorry. I modified your code in a way to output: 1*2*3*4*5. Can you modify your code so that the output will be: 5! = 5*4*3*2*1 = 120

import java.util.Scanner;

public class a4q1 {
     public static void main(String args[])
     {
           Scanner input =new Scanner (System.in);
           int num,factorial=1;
           String s="1";
          
           do
           {
          System.out.print("Please enter a number greater than or equal to 0.\n>>");
           num =input.nextInt();
           }
           while (num <0);
          
           for (int i =2; i <=num ; i++){
          factorial*=i;
          s += "*" + i;
          }
          
          System.out.println(num + "! = " +  s + "=" + factorial);       
    }
}
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.