Factorial - num = scan.nextInt(); //Make sure it's...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// **************************************************************** // Factorial.java // // Compute the factorial of a given number // // **************************************************************** import java.util.Scanner; public class Factorial { public static void main(String[] args) { int num; //number to find factorial of int fac; //factorial so far Scanner scan = new Scanner (System.in); //Get value to compute factorial of System.out.print("Please enter a nonnegative integer: ");
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: num = scan.nextInt(); //Make sure it's nonnegative while (num < 0) { System.out.println("Factorial is undefined for negative numbers."); System.out.print("Please enter a non-negative integer: "); num = scan.nextInt(); } //Compute and print factorial System.out.print(num + "! = "); fac = 1; while (num > 0) { fac *= num; num--; } System.out.println(fac); } }...
View Full Document

This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.

Ask a homework question - tutors are online