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]
2 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]2 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
Rosa is a high school freshman with no savings. She wants to buy a new smartphone in a few months for $300. Which account type b
Iteru [2.4K]
Simple savings account
6 0
2 years ago
Read 2 more answers
Describe your microsoft word skills that need to be improved upon the most.<br><br>​
Ilya [14]
Shortcuts & Formatting Tricks
Mail Merge
Table of Contents
Compare Documents
Track Changes
Using Synonyms
Presenting Tabular data
Managing Header & Footer
Adding Pictures at right position
Automating tasks through Macros
572 viewsView 4 Upvoters
Related Questions (More Answers Below)
5 0
3 years ago
The parts of a memo are _____.
Alex
The correct answer would be D
4 0
3 years ago
Read 2 more answers
Write a program that outputs "Hello World!" For ALL labs, end with newline (unless otherwise stated).
Vesnalui [34]

The program that output "Hello World " is represented as follows:

print("Hello World")

<h3 /><h3>Code explanation:</h3>

The code is written in python.

We use the print statement to print out the string word "Hello world".

When the code is run, the output of the code will be "Hello world".

"Hello world" is a string.

learn more on python code here: brainly.com/question/21497685?referrer=searchResults

8 0
2 years ago
pleeeease help!! I tried to turn on icloud photos to save storage and it’s been stuck at 1%. does anyone know why or how to fix
gtnhenbr [62]

Answer:

Explanation:

It just takes a very long time if you have too many videos or even very long ones. It took me 14 hours to download mine. You have to let it download though do not interrupt it! Very important!

3 0
3 years ago
Other questions:
  • Which Command Prompt commands in Windows is used for listing a computer connections to shared resources
    10·1 answer
  • Which of the following best describes the 7x7 PowerPoint rule
    8·1 answer
  • How can an individual find career data?
    13·1 answer
  • Monica is teaching herself to paint. What web resource would be the most helpful
    14·1 answer
  • What are the design concepts and assumptions behind a class, an object and the relationship between them? What are the roles met
    9·1 answer
  • In 3-5 sentences, describe how you would insert a graph in your word-processing document.
    13·1 answer
  • This OS was created by a developer named Torvalds.
    7·1 answer
  • Two time series techniques that are appropriate when the data display a strong upward or downward trend are ___________ and ____
    14·1 answer
  • There's a right and a wrong way to act on the internet. The right way includes practicing proper netiquette.
    15·2 answers
  • Organizations can face criminal or civil penalties for being ______ in protecting their sensitive data.
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!