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
  • I am taking a Intro Java course in college and for that, I need to make...

    I am taking a Intro Java course in college and for that, I need to make a Program to solve the following problem. Our program is going to analyze the sentiment of the nintendo switch on Amazon by first gathering the positive percentage and the negative percentage of the product by looking at the customer reviews, and then display rather it is negative or positive based on the # of stars. and my plans to solve the problem is down...

  • 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...

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

    I am a college student taking an INTRODUCTORY linux shell programming course and need some help with writing a for statement. How would I write a for statement to process all the arguments at the command line and indicate if the argument contains the name of a student in my class. The names of the students are in the file cis132students.   I realize you do not have the file but if you could write it I could see if it...

  • I am taking an introductory course in unix/linux shell programming and need some help. Can you...

    I am taking an introductory course in unix/linux shell programming and need some help. Can you tell me what command I would use to make the variable myName readable by child process

  • 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...

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