Question

In: Computer Science

There are two parts to this activity. Before you write a Java program for this activity,...

There are two parts to this activity. Before you write a Java program for this activity, write the pseudocode for this program. Turn in the pseudo code and Java program.

Create a program that prompts the user for an integer number. The program returns the factorial of the number. 0! = 1

In your program, answer the following question: What kind of loop are you using for this example? Counter-controlled loop or sentinel controlled loop? Explain.

Sample Input
Enter a number: 8

Sample Output
8! = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 40320
Sample Input
Enter a number: 3

Sample Output
3! = 3 * 2 * 1 = 6
Sample Input
Enter a number: 10

Sample Output
10! = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3628800
Sample Input
Enter a number: 0

Sample Output
0! = 1 
Sample Input
Enter a number: 5

Sample Output
5! = 5 * 4 * 3 * 2 * 1 = 120

Solutions

Expert Solution

Pseudocode

  • ​​​​​​input the number
  • fact =1
  • for i as n-1 to 0, fact=fact*i
  • print the resut using appropriate messages

Code


import java.util.Scanner;
public class fact {

  public static void main(String[] args)
   {
      int n;
      System.out.print("Enter a number: ");
      Scanner in = new Scanner(System.in);
      n =in.nextInt();
      System.out.print(n+"!=");
      int fact=1;
      for(int i=n;i>0;i--)
      { 
        if(i==n)
            System.out.print(n);
        else 
            System.out.print("*"+i);
        fact=fact*i;
        
      }
      if(n==0)
         System.out.print(fact);
      else 
         System.out.print("="+fact);
  }

}

Loop:

counter controlled loop is being used here. Counter controlled loop is nothing but number of iterations already known. here its obvious that the iteration have to done n times.If n is 7 iteration will occur 7 times.

Terminal work

.


Related Solutions

DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
Write the program in java Write a program that does basic encrypting of text. You will...
Write the program in java Write a program that does basic encrypting of text. You will ask the user the filename of a text file which contains a few sentences of text. You will read this text file one character at a time, and change each letter to another one and write out to an output file. The conversion should be done a -> b, b->c , … z->a, A->B, B->C, …. Z->A. This means just shift each letter by...
/* Problem 1 * Write and run a java program that prints out two things you...
/* Problem 1 * Write and run a java program that prints out two things you have learned * so far in this class, and three things you hope to learn, all in different lines. */ You could write any two basic things in Java. /* Problem 2 * The formula for finding the area of a triangle is 1/2 (Base * height). * The formula for finding the perimeter of a rectangle is 2(length * width). * Write a...
Write a program in JAVA that takes in two words, and then it recursively determines if...
Write a program in JAVA that takes in two words, and then it recursively determines if the letters of the first word are contained, in any order, in the second word. If the size of the first word is larger than the second then it should automatically return false. Also if both strings are empty then return true. YOU MAY NOT(!!!!!!!!!!): Use any iterative loops. In other words, no for-loops, no while-loops, no do-while-loops, no for-each loops. Use the string...
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two...
XML and JAVA Write a Java program that meets these requirements. It is important you follow...
XML and JAVA Write a Java program that meets these requirements. It is important you follow these requirements closely. • Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1. • In the Java file, print a welcome message that includes your full name. • The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT