Question

(in java) a. Include a good comment when you write the method described below: Write a...

(in java)

a. Include a good comment when you write the method described below: Write a method factorial which receives a positive integer n and calculates n! (n factorial), defined as follows: n!=1*2*…*(n-1)*n. For example, 5! = 1*2*3*4*5 = 120. The method should return this value to the calling program.

b. Write a driver program (main method) which will read an integer from the console, and pass it to the method to calculate its factorial. The main method then prints the original value and the calculated factorial.

Homework Answers

Answer #1
import java.util.Scanner;
public class FactorialLoop {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int n;
        System.out.print("Enter number: ");
        n = scanner.nextInt();
        int res = factorial(n);
        System.out.println(res);
    }

    private static int factorial(int n) {
        if(n<=0){
            return 1;
        }
        int res = 1;
        for(int i = 2;i<=n;i++){
            res *= i;
        }
        return res;
    }
}

Know the answer?
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
Similar Questions
Write Java program Lab42.java which takes as input two positive integers m and n and computes...
Write Java program Lab42.java which takes as input two positive integers m and n and computes their least common multiple by calling method lcm(m,n), which in turn calls recursive method gcd(m,n) computing the greatest common divisor of m and n. Recall, that lcm(m,n) can be defined as quotient of m * n and gcd(m,n).
(Java) Write the missing equals method needed for the Point class shown on the screen to...
(Java) Write the missing equals method needed for the Point class shown on the screen to work with this program. The missing method would be added to the Point class. class PointTestP3 {   public static void main(String[] args) {    Point p1 = new Point(5, 6);     Point p2 = new Point(5, 6);        System.out.println(p1.equals(p2)); // prints "true"   } }
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
Solve the following using java Write a program that runs three threads, each thread randomizes a...
Solve the following using java Write a program that runs three threads, each thread randomizes a number between 1 and 100. The main thread waits for all the others to finish, calculates the maximum of the numbers, which were randomized, and prints it. Random number 1: 10 Random number 2: 38 Random number 3: 81 Max: 81 Note: You should create 3 threads in adition to the main thread. Also, you can use a single thread class and create 3...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same. 2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values. 3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e.,...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: 1. Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These same mechanisms can be used as a part of writing a simple compiler. Write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT