Ask Question
22 November, 11:58

1. Implement a method factorial (int n), that takes a number (int) and returns its factorial. Factorial of an integer is the product of all positive integers less than or equal to it. Method needs to be implemented using recursion.

+3
Answers (1)
  1. 22 November, 15:50
    0
    import java. io.*;

    import java. util.*;

    public class Main

    {

    public static int factorial (int n) {

    if (n = = 0)

    return 1;

    else

    return (n * factorial (n-1)); / /recursively calling factorial function

    }

    public static void fibbonaci (int n)

    {

    if (n>0)

    {

    c=a+b; / /next term in series

    a=b;

    b=c;

    System. out. print (" "+c);

    fibbonaci (n-1); / /recursively calling fibbonaci function

    }

    }

    static int a=0, b=1, c;

    public static void main (String[] args) {

    int list_num, fact;

    List num=Arrays. asList (1,3,5,7,9); / /arraylist with 1,3,5,7,9

    for (Integer i:num) / /for-each loop

    {

    a=0;

    b=1;

    list_num=i. intValue ();

    System. out. println ("Fibbonaci series for "+list_num);

    System. out. print (a+" "+b);

    fibbonaci (list_num-2); / /calling fibbonaci for values in list

    fact=factorial (list_num); / /calling factorial for values in list

    System. out. println ();

    System. out. println ("Factorial for "+list_num+" is "+fact);

    System. out. println ();

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “1. Implement a method factorial (int n), that takes a number (int) and returns its factorial. Factorial of an integer is the product of all ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers