Java Loop Exercise: The user enters a number within 10 to calculate the factorial of the number by operation

Source: Internet
Author: User

Package Practicego;import java.util.scanner;/* * 4. The user enters a number within 10, and the factorial of the number is calculated by Operation */public class Cto {public static void main (string[] args) {Scanner sc = new Scanner (system.in); System.out.println ("Please enter a number within 10, calculated by the system factorial"); int num = Sc.nextint (); int result = 1;int store = num;//Avoid direct manipulation of input values for (int i=0 ; i<num; i++) {result *= store;store--;} System.out.println (num+ "factorial is:" +result);}}

Test:

Please enter a number within 10, and the factorial of factorial 00 is calculated by the system: 1

Mathematically stated: 0! = 1 with the above code just ok!

Please enter a number within 10, and the factorial of factorial 11 is calculated by the system: 1
Please enter a number within 10, and the factorial of factorial 55 is calculated by the system: 120


Java Loop Exercise: The user enters a number within 10 to calculate the factorial of the number by operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.