+ 1

An interesting code

What's your idea about my code? It calculates the factorial of your integer input. Appreciate your opinions! :) import java.util.Scanner; public class Calculation{ int factorial(int input){ int product= 1; int x= input; for(int z=0; z<input;z++){ product*=x; x--; } return product; } public static void main(String[] args){ Scanner input= new Scanner(System.in); Calculation o1= new Calculation(); System.out.println(o1.factorial(input.nextInt())); } }

7th Jul 2017, 6:22 PM
Moji 99
Moji 99 - avatar
3 Antworten
+ 3
Nice job. 👌 PS: If you don't want to have to create an object to do the calculation you can make the factorial method static and call it directly.
7th Jul 2017, 6:44 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
You can exclude X: int factorial(int input) { int product = 1; for(int z = 1; z <= input; z++){ product *= z; } return product; }
7th Jul 2017, 7:06 PM
Chomitch
Chomitch - avatar
0
Thank you guys 👍🏻
7th Jul 2017, 8:53 PM
Moji 99
Moji 99 - avatar