Question 1: Number factorial

I'm in good spirits today, I did the question again, and left the answer for easy reference during the interview with my peers. Be careful not to get caught.

 

This can be recursive, or it can be written in ordinary way. Here is the ordinary way of writing. Note that JDK1.8 is used:

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

/**
 * 1. A digital output factorial
 */
public class Test1 {
    public static void main(String[] args) throws IOException {
        System.out.println( "Enter a number: " );
         try (InputStream inputStream = System.in;) {
            Scanner sc = new Scanner(inputStream);
            int number = sc.nextInt();
            System.out.println();
            int result = 1;
            for(int i=number;i>0;i--){
                result = result*i;
            }
            System.out.println( "The factorial is: "+ result);
        }
        System.exit(0);
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325680576&siteId=291194637