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
When you make taffy (a pliable candy), you must heat the candy mixture to 270 degrees Fahrenheit. Write a program that will help
hichkok12 [17]

Answer:

The solution code is written in Python 3.

  1. temp = int(input("Enter current temperature (Fahrenheit): "))
  2. while(temp < 270):
  3.    temp = int(input("Enter current temperature (Fahrenheit): "))
  4.    
  5. if(temp > 330):
  6.    print("You burned the taffy!")
  7. else:
  8.    print("Your taffy is ready for the next step")

Explanation:

Firstly, we can try to get a first temperature reading (Line 1)

if the first reading is smaller than 270, keep prompting user for the next reading using while loop (Line 3 - 4)

if the input temperature is bigger or equal to 270, the program will exist the while loop and proceed to check if the final temperature reading is bigger than 330 to determine an appropriate message to display (Line 6 - 9).

3 0
3 years ago
A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the el
ludmilkaskok [199]

Answer:

Following are the expression of the given statement.

tictactoe[0][0] == tictactoe[1][0] && tictactoe[1][0] == tictactoe[2][0]

Explanation:

In the following scenario, there is the two dimensional or matrix array of integer data type 'tictactoe' which has a total of nine elements then, after that we have to define the diagonal of the array variable which comprises the very 1st element of the 1st row are equal. So, the following expressions are true according to the scenario.

8 0
3 years ago
Which file system is designed to verify and autocorrect data faults on the volume without having to bring the volume down for ma
Aloiza [94]

Answer: ReFS or Resilient File System.

The ReFS system was actually built from the NTFS (New Technology File System). The ReFS has similar features, but also comes with a built-in scanning system. The ReFS constantly checks your data constantly for corrupted data. The ReFS also supports larger volumes of drives and can support up to 32,768 characters.

6 0
3 years ago
True or false that computers that are joined together are able to share hardware and software, but not data
kozerog [31]
False computers are able to share hardware and software but not data
8 0
3 years ago
We can use formatting before and after typing.​
Anika [276]

Answer:

i dont know but thanks for marks

8 0
3 years ago
Other questions:
  • The ______ identifies the path for the currently open folder
    8·2 answers
  • What is really meant by SSDs
    9·1 answer
  • Why is it a good idea to leave an interview being courteous and polite?
    10·1 answer
  • Which one is not the future of wireless technology?
    8·1 answer
  • What is an Apple Pen?
    5·2 answers
  • Which of the following was the primary purpose of muckraking, sensationalism, and yellow journalism in the early 1900s?
    11·1 answer
  • How many sets of number do both Hex and RGB values have?
    10·1 answer
  • Explain the evolution of programming language​
    15·1 answer
  • At what layer in the TCP/IP protocol hierarchy could a firewall be placed to filter incoming traffic by means of:
    5·1 answer
  • Viruses that load from usb drives left connected to computers when computers are turned on are known as.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!