Ask Question
22 July, 02:40

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.

+5
Answers (1)
  1. 22 July, 03:00
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers