Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Factorial {
- public static int facIter(int n) {
- int res = 1;
- while (n > 1) {
- res = res * n;
- n--;
- }
- return res;
- }
- public static int facRecur(int n) {
- if (n <= 1) {
- return 1;
- }
- else {
- return n * facRecur(n-1);
- }
- }
- public static void main(String[] args) {
- Scanner kb = new Scanner(System.in);
- System.out.print("Enter a line of pascals triangle: ");
- int input = kb.nextInt();
- System.out.println("Iterative:\t" + facIter(input));
- System.out.println("Recursive:\t" + facRecur(input));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement