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
cell d1 contains the value 7.877 you want cell d1 to display this value as 7.9 how can you accomplish
Georgia [21]

Answer:

You round?

Explanation:

6 0
4 years ago
Operating software is a must in every software (a) True (b) False
AveGali [126]

Answer:

False

Explanation:

Some bare metal do not need an OS at all.

4 0
3 years ago
Write six causes of data lost
just olya [345]
<span>1. Deleting files accidentally
</span>

2. Viruses and damaging malware

3. Mechanical damages of hard drive

4. Power failures

5. Theft of computer

6. Spilling coffee, and other water damages

7. Fire accidents and explosions

<span>Hope this helps.</span>
3 0
4 years ago
Design a class named Cake. Data fields include two string fields for cake flavor and icing flavor and numeric fields for diamete
kkurt [141]

Using the computer language in pseudocode to write a function code that  declares two Cake objects and sets and displays their values.

<h3>Writting the code in pseudocode:</h3>

<em>- cake flavor: string</em>

<em>- icing flavor: string</em>

<em>-diameter: num</em>

<em>-price: num</em>

<em>+set cake flavor (cake flavor : string) : void</em>

<em>+set icing flavor (icing flavor: string) : void</em>

<em>+set Diameter(size : num) : void</em>

<em>+set Price(price : num) : void</em>

<em>+get cake flavor () : string</em>

<em>+get cing flavor () : string</em>

<em>+get Diameter() : num</em>

<em>+get Price() : num</em>

<em>Answer A=</em>

<em>Pseudocode:</em>

<em>class Cake</em>

<em>Declarations</em>

<em>private string cake flavour</em>

<em>private string icing flavor</em>

<em>private num diameter</em>

<em>private num price</em>

<em>public void set Cake flavour (string Cake flavour)this. Cake flavour = Cake flavour</em>

<em>return</em>

<em>public void set icing flavour (string icing flavour)this. icing flavour = icing flavour</em>

<em>return</em>

<em>public void set Diameter(num size)</em>

<em>diameter = size</em>

<em>return</em>

<em>public void set Price(num price)</em>

<em>this price = price</em>

<em>return</em>

<em>public string get Cake flavour ()</em>

<em>return Cake flavour</em>

<em>public string get icing flavour ()</em>

<em>return icing flavour</em>

<em>public num get Diameter()</em>

<em>return diameter</em>

<em>public num get Price()</em>

<em>return price</em>

<em>end Class</em>

<em>start</em>

<em>Declarations</em>

<em>Cake my Cake</em>

<em>Cake your Cake</em>

<em>myCake.set cake flavour (“chocalate”)</em>

<em>myCake.set icing flavour (“peppermint”)</em>

<em>myCake.setDiameter(15)</em>

<em>myCake.setPrice(18)</em>

<em>myCake.set cake flavour (“pineapple”)</em>

<em>myCake.set icing flavour (“orange ”)</em>

<em>yourCake.setDiameter(20)</em>

<em>yourCake.setPrice(25)</em>

<em>output “Cake 1 info:”</em>

<em>output myCake.get cake flavour ()</em>

<em>output myCake.get icing flavour ()</em>

<em>output myCake.getDiameter()</em>

<em>output myCake.getPrice()</em>

<em>output Cake 2 info:”</em>

<em>output yourCake.get cake flavour ()</em>

<em>output yourCake.get icing flavour ()</em>

<em>output yourCake.getDiameter()</em>

<em>output yourCake.getPrice()stop</em>

See more about pseudocode at brainly.com/question/13208346

#SPJ1

5 0
2 years ago
Question 4
Tcecarenko [31]

Answer: squared ← number * number

Explanation:

4 0
3 years ago
Other questions:
  • 1. Which markup language adds the ability to use video without requiring the user to download add-ons?
    13·1 answer
  • The network board in a workstation is currently configured as follows:- network speed = auto- duplexing = autoThe workstation is
    13·2 answers
  • What is one way in which tablets differ from laptops and notebooks
    12·2 answers
  • Which data table display compares report metrics to the website average?
    6·1 answer
  • If a device does not require a driver, it is _____.
    6·1 answer
  • Can you find the ip address from a physical address
    13·1 answer
  • Microsoft Windows is the least used operating system. TRUE or FALSE.
    13·1 answer
  • What makes up data encryption standard ?
    9·1 answer
  • Write a small program that takes in two numbers from the user. Using an if statement and an else statement, compare them and tel
    12·1 answer
  • Which of the following tiny computer apps is designed to be useful but could cause more harm than good?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!