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
Which of the following is a common financial aid scam that students sometimes find on the Internet? Grant and scholarship databa
allsm [11]

Which of the following is a common financial aid scam that students sometimes find on the internet?

All of the above.

6 0
3 years ago
Read 2 more answers
A ________ is a material deficiency, or combination of significant deficiencies, that results in more than a remote likelihood t
VLD [36.1K]

Answer:

a.material control failure

Explanation:

A material control failure is a material deficiency, or combination of significant deficiencies, that results in more than a remote likelihood that a material mis-statement in the annual or interim financial statements will not be prevented or detected.

7 0
3 years ago
Converting raw data into a more meaningful form is called:
White raven [17]
Converting raw data into a more meaningful form is called: processing
8 0
3 years ago
What is the purpose of the Excel Function Reference?
Citrus2011 [14]

Answer:

To look up functions and their purposes

Explanation:

Edg

3 0
3 years ago
List any two different between primary memory and secondary memory.​
nikitadnepr [17]

Answer:

Primary memory is directly accessible by Processor/CPU. Secondary memory is not directly accessible by the CPU. ... The memory devices used for primary memory are semiconductor memories. The secondary memory devices are magnetic and optical memories

3 0
3 years ago
Read 2 more answers
Other questions:
  • You can use Facebook's live feed tool to broadcast content as you post it
    8·2 answers
  • To display measurements on the ruler while changing column widths, hold down the ________ key while dragging the marker.
    13·1 answer
  • You will be given a string, containing both uppercase and lowercase alphabets(numbers are not allowed).
    14·1 answer
  • When was internet started in which year​
    13·2 answers
  • Write a program with a method computeCommission which takes a double that is the salesAmount and returns the commissions for sal
    9·1 answer
  • Which of the following specific components are incorporated on HDInsight clusters?
    13·1 answer
  • Tradegy deals with _____
    14·1 answer
  • How do you change the order of the slides in your presentation in the slide pane 
    15·2 answers
  • Which privacy protection uses four colors to indicate the expected sharing limitations that are to be applied by recipients of t
    12·1 answer
  • What are good reasons to do yearly disaster recovery testing? check all that apply
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!