import java.util.Scanner; interface Factorial { int calculate(int n); } public class LambdaFactorial { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int n = scanner.nextInt(); Factorial factorial = (num) -> { int result = 1; for (int i = 2; i <= num; i++) { result *= i; } return result; }; System.out.println("Factorial of " + n + ": " + factorial.calculate(n)); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter