Question

I am currently taking a Foundation of Programming course where we are being taught Java. We...

I am currently taking a Foundation of Programming course where we are being taught Java. We were shown how to make a program to calculate factorials recursively, how could I create an iterative method that could calculate factorials?

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

Any problem which can be solved by recursion can be solved by iteration as well and vice versa. The factorial of a number is the product of all positive numbers which are less than of equal to the number. To calculate factorial of a number n, in iterative manner a simple for loop can be used which will multiply all positive numbers less than of equal to n.

Below is the JAVA program for the same.

I have documented the code. Hope it helps.

Cheers!!  

****************************************************************************************************************************************


public class solution
{
public static void main(String args[])
{
// Line below initializes the number for which factorial needs to be calculated
// modify the value as per your desire
int inputNumber = 6;

// since the value of factorial can be very large the datatype for factorial is long
// it is initialized to 1 so that numbers can be multiplied to it
long factorial = 1;

// now we implement the for loop. The loop counter 'i' is initialized to the input number
// and is decremented in each iteration until it is reduced to 1 and in each iteration
// we multiply the current value of i to the variable 'factorial' so that by the end of all
// iterations it will store the final result
for(int i = inputNumber; i>=1 ; i--)
{
// multiplying the value of i to factorial
factorial = factorial*i;
}

// printing the final output
System.out.println("Factorial of given number is:"+factorial);
}
}

Add a comment
Know the answer?
Add Answer to:
I am currently taking a Foundation of Programming course where we are being taught Java. We...
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
  • Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggli...

    Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo {     public static Random r = new Random(58);     public static void main(String[] args) {         char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100);         displayBoard(startNavigation(currentBoard))     }     public static int[] getRandomCoordinate(int maxY, int maxX) {         int[] coor = new...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • Can you help me? I am taking an INTRODUCTORY unix/linux shell programming course and need to...

    Can you help me? I am taking an INTRODUCTORY unix/linux shell programming course and need to know what the following command will do. Need a SIMPLE answer please. onsystem john1 &

  • I am a college student taking an intro to linux shell programming course and need some...

    I am a college student taking an intro to linux shell programming course and need some help with this question. I need to write an until statement as indicated: Check to see if the file cis132 exists in the current directory. If it does not a message will be printed every 10 seconds indicating that file does not exist. Once the file is created the message will stop. Thanks for your help.

  • Hello, we are currently learning the compareTo() method in my Java Data Structures class. I am...

    Hello, we are currently learning the compareTo() method in my Java Data Structures class. I am having trouble understanding what the output here will be: a = "ballD"; b= "ballDs"; System.out.println(a.compareTo(b)); I don't know how to interpret the small "s". I know that if it was a = "ballD"; b = "ballD"; the output would be 0. Please help, thanks in advance

  • I am taking an INTRODUCTORY Windows OS scripting course and could use your help in completing...

    I am taking an INTRODUCTORY Windows OS scripting course and could use your help in completing this homework assignment: I need to make sure that my batchfile directory is automatically included in the search path every time I open a command prompt window. My answer must be a step-by-step explanation (bulleted or numbered list) of the EXACT approach I took to complete this task. I must also provide an explanation of EXACTLY how I tested this to make sure it...

  • I am taking a computer science course and my professor teaches much too fast. I am...

    I am taking a computer science course and my professor teaches much too fast. I am feeling so overwhelmed and lost. Very worried as well, as I am trying my very hardest in this class but just cant seem to grasp it. I have just been given this assignment.... Can someone explain what I am supposed to do? Is there one class? two? are there supposed to be multiple files? :( thanks! Write a Java class called a Circle that...

  • Hello, I am currently working on a case study for my finance course and I do...

    Hello, I am currently working on a case study for my finance course and I do not know what the following question means. I am unsure how to approach it and am looking for any guidance as to how I can start: "Discuss with respect to asymmetry of information how large you believe the underpricing of the IPO should be." Where should I look for necessary info?

  • Looking at Java FX, scene builder, CSS, and widgets, we begin to see some of the...

    Looking at Java FX, scene builder, CSS, and widgets, we begin to see some of the graphics capabilities of Java. We have looked at interfaces, of course, but were limited in the ways we could make them more appealing, dynamic, or interactive. What can you foresee being able to create using some of these tools in conjunction with others we've learned throughout the term?

  • I am learning programming in JAVA. Please help me with this programming assignment. Your lab assignment...

    I am learning programming in JAVA. Please help me with this programming assignment. Your lab assignment this week is to build a little math program to help kids practice their addition and subtraction. You will create a menu and have the kid select what kind of math problem they want to solve. Once the kid selects what kind of problem. Randomly generate two numbers, display the problem and allow the kid to enter the answer. If the answer is correct....

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
Active Questions
ADVERTISEMENT