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
Information about www
iragen [17]

Answer:

Here is your ans

Thanku you

4 0
3 years ago
Varied amount of input data Statistics are often calculated with varying amounts of input data. Write a program that takes any n
Gnom [1K]

In Python 3.8:

nums = list(map(int, input("Enter your numbers space separated: ").split()))

print(f"The largest number is {max(nums)} and the average of all the numbers entered is {sum(nums)/len(nums)}")

7 0
3 years ago
The mechanism for preventing traffic that is perceived to be dangerous to the network is known as a a0.
nikklg [1K]
I would go with firewall
6 0
3 years ago
On Brainly, how can I change my username? from halfsidepancake​
klio [65]

Answer:

unfortunately you cant change your username.... i tried 2 and the only way is to make a new account

Explanation:

8 0
3 years ago
Read 2 more answers
HURRY IM TIMED Note that common skills are listed toward the top, and less common skills are listed toward the bottom. According
Sloan [31]

Answer:

1 and 3 are the answers

4 0
3 years ago
Read 2 more answers
Other questions:
  • Introduction to graphic design please help! Define the four terms: design ,art,decoration , and visual literacy. Describe how th
    12·1 answer
  • What is a sluggish beta signal detection theory?
    15·1 answer
  • Consider the following relational schema, where the primary keys are Bold,
    7·1 answer
  • A(n) _____ uses spatial and nonspatial data and specialized techniques for storing coordinates of networks of lines (roads, rive
    8·1 answer
  • Which VPN topology is also known as a hub-and-spoke configuration?
    15·1 answer
  • Which is one use for a hyperlink? A. to create a heading style B. to animate important text C. to create a link to a website. D.
    7·1 answer
  • In a paragraph of no less than 125 words, explain what netiquette is and how it improves efficiency and productivity in the work
    14·1 answer
  • You have imported a library with the birthMonth() function. Based on the API, how many strings are inputed to calculate the birt
    11·1 answer
  • How do you enter the decimal 73 into the computer?
    11·1 answer
  • Question 3 of 25 In computer science, what is a developer? A. A person who organizes all the details of a project B. An individu
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!