1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
tatuchka [14]
3 years ago
6

Write a static method that implements a recursive formula for factorials. Place this method in a test program that allows the us

er to enter values for n until signaling an end to execution.
Computers and Technology
1 answer:
matrenka [14]3 years ago
4 0

Answer:

Written in Java

import java.util.*;

public class Main {

  public static int fact(int n) {

     if (n == 1)

        return n;

     else

        return n * fact(n - 1);

  }

  public static void main(String[] args) {

     int num;

     Scanner input = new Scanner(System.in);

     char tryagain = 'y';

     while(tryagain == 'y'){

     System.out.print("Number: ");

     num = input.nextInt();

     System.out.println(num+"! = "+ fact(num));

     System.out.print("Try another input? y/n : ");

     tryagain = input.next().charAt(0);

}        

  }

}

Explanation:

The static method is defines here

  public static int fact(int n) {

This checks if n is 1. If yes, it returns 1

     if (n == 1)

        return n;

If otherwise, it returns the factorial of n, recursively

     else

        return n * fact(n - 1);

  }

The main method starts here. Where the user can continue executing different values of n. The program keep prompting user to try again for another number until user signals for stoppage

  public static void main(String[] args) {

This declares num as integer

     int num;

     Scanner input = new Scanner(System.in);

This initializes tryagain as y

     char tryagain = 'y';

This checks if user wants to check the factorial of a number

     while(tryagain == 'y'){

This prompts user for input

     System.out.print("Number: ");

This gets user input

     num = input.nextInt();

This passes user input to the function and also prints the result

     System.out.println(num+"! = "+ fact(num));

This prompts user to try again for another value

     System.out.print("Try another input? y/n : ");

This gets user response

     tryagain = input.next().charAt(0);

}        

Download txt
You might be interested in
Which option helps you choose the design of a slide in a presentation A) slide layout b) shapes c) animation d) slide transition
mihalych1998 [28]

Answer:

B) slide layout

Explanation:

Slide layout, since you can choose what type of design you want, I'm guessing this is for power point.

8 0
3 years ago
Read 2 more answers
Which of the following is not a language commonly used for web programming?
DerKrebs [107]
The answer would be Assembly language as assembly language is not used for web development, but rather is a low level programming language.

Please mark branliest if this helped!!
7 0
3 years ago
Read 2 more answers
A method which applies to the class in which it is declared as a whole and not in response to method calls on a specific object
Mandarinka [93]

Answer:

D) Static method

Explanation:

A static method is a method that is created only for the class and not it's instance variables or objects. Such methods can only be accessed by calling them through the class's name because they are not available to any class instance. A static method is created by prefixing the method's name with the keyword static.

A static method cannot be overridden or changed. You create a static method for a block of code that is not dependent on instance creation and that can easily be shared by all instances or objects.

5 0
3 years ago
Which of the following is an example of self-awareness?
DENIUS [597]
Are you sure this is the right subject
4 0
3 years ago
What sort of negative outcomes are possible for this type of risk?
Harlamova29_29 [7]
More details please?
6 0
3 years ago
Read 2 more answers
Other questions:
  • Let's say you want to insert a photo of the Microsoft PowerPoint Ribbon to put into your presentation. Which of these should you
    6·1 answer
  • Programmers refer to a sequence of characters as a ____.
    12·1 answer
  • 4. What will the following line of code produce?
    13·1 answer
  • Select the instances in which you should include a comma.
    13·1 answer
  • The find and
    10·1 answer
  • A device can transport objects instantaneously anywhere in the world without damaging them. Is it a creative invention? Yes, bec
    6·1 answer
  • How do I create a videogame
    11·1 answer
  • At one time, a station sign-off test pattern was designed so TV owners could adjust the quality of their picture and sound
    9·1 answer
  • Company A’s IT department has a hosting platform specifically for systems used by the company’s large marketing department. This
    11·1 answer
  • What is a cell? how is it referred?​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!