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
A(n) ________ CPU has two processing paths, allowing it to process more than one instruction at a time.
lions [1.4K]
C. Dual-core is the answer
3 0
3 years ago
Read 2 more answers
When you want to avoid sending email that a recipient may feel their privacy has been invaded, how would you fill in the (To) bo
timama [110]
Fill out the name or business address of the place they work or if they do not have a business email send it to a friend
6 0
2 years ago
Define Rule Of Thirds
Katena32 [7]

Answer:

In photography, the rule of thirds is a type of composition in which an image is divided evenly into thirds, both horizontally and vertically, and the subject of the image is placed at the intersection of those dividing lines, or along one of the lines itself.

6 0
2 years ago
Create a function named first_a that uses a list comprehension. The function will take a single integer parameter n. Find every
aivan3 [116]

Answer:

def first_a(n):

   lst1 =[x for x in range(1,n+1)if x%6==0 or x%11 ==0]

   print(lst1)

Explanation:

Above is a function in python programming language. We have used list comprehension to check numbers that are multiples of 6 or 11 in a range.

When this function is called it will receive an argument (n) of type integer, a range will then be generated from 1 to n+1 since n is inclusive. The modulo operator is used to determine is a value is a multiple of 6 or 11, since their multiples will evaluate to 0

3 0
3 years ago
Which of the following is an accurate statement? When the LOWER function is used in a SELECT clause, it will automatically store
quester [9]

Answer:

Hi there! The answer is C:

Explanation:

The scope of the LOWER function is limited to the select clause it is being used in. Outside of the select statement, the column will be returned as it is currently present in the database. Using the LOWER function will not automatically store data in lower case letters in the database tables. As an exercise, try creating a simple database table called "Employees" with columns "id" and "name". Then input a row using an insert statement with id as 1 and name as "BLAH". Try using the select statement with LOWER function and then without the LOWER statement.

5 0
2 years ago
Other questions:
  • Jackie is planning a birthday party for her little brother and is researching different trampoline parks. She receives a pop-up
    12·2 answers
  • Arpanet was developed by the united states so that there was a communication network that would survive a nuclear war. True or F
    11·1 answer
  • Write an examples of Output device, storage devices and inputs device 10 each excluding the common ones​
    5·1 answer
  • Is this statement true or false?
    12·2 answers
  • Jeremy wants to run a program file from the command prompt. Which of the following should Jeremy type in the command prompt wind
    8·2 answers
  • Can you use public domain images without violating copyright laws?
    5·1 answer
  • Please answer <br><br> Steps 1-6 <br><br> Visual basics
    7·1 answer
  • The first step to accurate coding is to identify the ___________ in the diagnostic statement.
    7·1 answer
  • What is a typical use for a MAN?
    13·1 answer
  • Software piracy is acceptable as it helps us obtain software cheaper or sometimes even for free.
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!