Question

Programming JAVA to provide an iterative and recursive solution ( tow separate methods) for calculating the...

Programming JAVA to provide an iterative and recursive solution ( tow separate methods) for calculating the factiorial of a number. The factorial of a number is denoted by, for example 5! = 5 * 4 * 2 * 1.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Factorial.java

//This package is imported to use scanner class
import java.util.*;
public class Factorial
{
//Method to find factorial of number using iteration. Here for loop is used for iteration.
int factorial1(int n1)
{
int fact=1;
for(int i=1;i<=n1;i++)
{
fact=fact*i;
}
return fact;
}
//Method to find factorial of number using recursion.
int factorial2(int n2)
{
if(n2==0)
return 1;
else
return (n2*factorial2(n2-1));//recursively calling
}
public static void main(String args[])
{
//scanner class object is first created to take inputs from users.
Scanner sc=new Scanner(System.in);
Factorial f1=new Factorial();
System.out.println("Enter a number to find factorial of the number using iteration:");
//Reading input from user using scanner class object
int n1=sc.nextInt();
int fact1=f1.factorial1(n1);
System.out.println("The factorial of the number calculated using iteration is:"+fact1);
System.out.println("------------------------------------------------------------------");
Factorial f2=new Factorial();
System.out.println("Enter a number to find factorial of the number using recursion:");
//Reading input from user using scanner class object
int n2=sc.nextInt();
int fact2=f2.factorial2(n2);
System.out.println("The factorial of the number calculated using recursion is:"+fact2);
}
}

Output Screenshot:

Note: Every step is explained using comments in the program. Please save the file as Factorial.java and compile and execute the program using commands shown in the screenshot.

Add a comment
Know the answer?
Add Answer to:
Programming JAVA to provide an iterative and recursive solution ( tow separate methods) for calculating the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code is to be written in java. a. Construct a RECURSIVE solution for following iterative solution...

    Code is to be written in java. a. Construct a RECURSIVE solution for following iterative solution to find a value in a circular-linked list b. prove the correctness of your recursive solution by induction /** @param value - a value to search for @return true if the value is in the list and set the current reference to it otherwise return false and not updating the current reference */ public boolean find(T value){ if(this.cur == null) return false; //get out,...

  • Programming Language: Java Please write a program that uses a recursive algorithm to compute the determinant...

    Programming Language: Java Please write a program that uses a recursive algorithm to compute the determinant of a matrix. It should read the order of the matrix, read the matrix, print it out, compute, and print the determinant. Your program should be able to evaluate multiple matrices on a single execution. For class purposes, your program should handle matrices up to and including those of order 6. You are required to use an array for this problem. Your solution must...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Java... Write a class that has three overloaded static methods for calculating the areas of the...

    Java... Write a class that has three overloaded static methods for calculating the areas of the following geometric shapes: • circles -- area = π*radius^2 (format the answer to have two decimal places) • rectangles -- area = width * length • trapezoid -- area = (base1 + base2) * height/2 Because the three methods are to be overloaded, they should each have the same name, but different parameters (for example, the method to be used with circles should only...

  • Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number...

    Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number which is defined as follows: fibo(0) = 0 fibo(1) = 1 fibo(n) = fibo(n-1) + fibo(n-2) for n >= 2 Question 2. Write a recursive function that calculates the sum of quintics: 1power of5 + 2power of5 + 3power of5 + … + n5 Question 3. Write a program to find a route from one given position to another given position for the knight...

  • In Java code provide the desired Big O notation methods and then call those methods in...

    In Java code provide the desired Big O notation methods and then call those methods in the main. Problem: Let n be the length of an integer array a, for each of the following classes ?(log(log(log(?)))) ?([log(?)] 3 ) ?(? 5 ) ?(4 ? ) ?(?!) ?(? ? ) write a method that takes as sole input an array a of length n, non-recursive, whose running time belongs to only one of the above classes.

  • Please write in java Write a main program that runs the following two recursive methods demonstrating...

    Please write in java Write a main program that runs the following two recursive methods demonstrating that they work. #1) Write a recursive method writeSquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending order. For example • writeSquares(8): prints . 49 25 9 1 4 16 36 64 • writeSquares(11); prints 121 81 49 25 9 1 2 16 36 64 100 •...

  • Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should...

    Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should meet the following requirements: 1. The program shall graphically prompt the user for a file. 2. The program shall read the selected file which will contain 1 integer per line. 3. The program shall sort the values it reads from the file from largest to smallest. 4. The program shall write the values to an output file from largest to smallest in the same...

  • This needs too be in java programming language 2 Write a recursive method that parses a...

    This needs too be in java programming language 2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static int hex. 2 Dec (String hexstring) Write a pensare cam that that prompts the user to enter a hex, string & displays its decimal equivalent. Recalls Anc=1010566-110. z 490 Use the following her values to convert: BAD, BAC98, BabA73

  • In java need help with the TODO sections creating recursive methods public class CountUpDown { /**...

    In java need help with the TODO sections creating recursive methods public class CountUpDown { /** * countUp - a recursive function that counts up from 1 to n * * @param n the integer value to count up to */ private static void countUp(int n) { // TODO PRELAB // IMPLEMENT THIS RECURSIVE METHOD } /** * countDown - a recursive function that counts down from n to 1 * * @param n the integer value to count down...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT