Question

In: Computer Science

I need to create a servlet that takes in an input that will do the following...

I need to create a servlet that takes in an input that will do the following calculations.

  1. If the input number is a prime, just output a message showing that it is a prime;
  2. If the input number is not a prime and it is an odd integer, write it as a product of two smaller odd integers;
  3. If the input number is not a prime and it is an even integer, first extract the largest power of 2, then for the remaining odd number, try to factorize it if you can. For your output, you give the power of 2 as one number and one more factor if this factor is a prime, or two more factors if you can still factorize the odd number after extracting the power of 2.

Solutions

Expert Solution

// suppose we have a simple html form which take input number

<html>
<body>
<form action="MyServlet" method="Get">
Enter the Number:<input type="text" name="n1"/><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

// servlet program

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String x=request.getParameter("n1");       //take input from html
int n=Integer.parseInt(x);     // parse string data into integer


int i=1,fact=1;

        int m=n/2
      for(i=2;i<=m;i++)
        {
          if(n%i==0)
            {
                printf("Number is not prime");
                flag=1;

                if(n/2==0)

               {

                 printf("number is even");

               }

                else

                {

                  printf("number is odd")

                }
                break;
            }
        }
       if(flag==0)
       printf("Number is prime");   
       }
}
}


Related Solutions

I need someone to create a program for me: Create a program that takes as input...
I need someone to create a program for me: Create a program that takes as input an employee's salary and a rating of the employee's performance and computes the raise for the employee. The performance rating here is being entered as a String — the three possible ratings are "Outstanding", "Acceptable", and " Needs Improvement ". An employee who is rated outstanding will receive a 10.2 % raise, one rated acceptable will receive a 7 % raise, and one rated...
Matlab Code that does the following: Takes as input 3 frequencies. Create a signal that is...
Matlab Code that does the following: Takes as input 3 frequencies. Create a signal that is a mixture of the 3 signals. Create a bandpass filter that only selects the center frequency. Output the filtered signal which contains only the middle frequency. Plot in time and frequency domain the original signal and the filtered signal. Show the output for filter order 1 and 15. Upload a pdf of the image files. Each figure should have your name in the title...
Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you...
Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you “ChattBank” Project. This MyServlet will display a message like “Go Braves” in a simple <h1> tag. Run this servlet from a Browser window by typing in the servlet name in the URL line. (ie. http://localhost:8080/ChattBank/MyServlet). Make sure that your Server is up and running before you test this Servlet. The best way to do this is just Run your “ChattBank” Project once before you...
Directions: Create a program that takes as input an employee's salary and a rating of the...
Directions: Create a program that takes as input an employee's salary and a rating of the employee's performance and then computes the appropriate raise for the employee based on that rating. The performance rating is entered into the program as a String. The three possible ratings are "Outstanding", "Acceptable", and "Needs Improvement". An employee who is rated as Outstanding will receive a 10.2% raise, one rated Acceptable will receive a 7% raise, and one rated as Needs Improvement will receive...
Create a ValueGet() method that takes a linked list as input and an integer index and...
Create a ValueGet() method that takes a linked list as input and an integer index and returns the value stored in the node at that index position. Sample Input: 01->100->300->214, index = 2 Output: 300 At index 2 the node has a value of 300 give me the full code. Give the full code with c++
Create a Java method that takes a String as input value and returns the number of...
Create a Java method that takes a String as input value and returns the number of vowels contained in that string.
Write a program to do the following. • Input an integer n. • Create a BST...
Write a program to do the following. • Input an integer n. • Create a BST S inserting the keys 1, 2, . . . , n in that order, which will result in a completely-skewed tree. • Measure the time to search for n + 1 in S. • Display the time taken for search. /** * Exception class for access in empty containers * such as stacks, queues, and priority queues. * @author Mark Allen Weiss */ public...
Write a program to do the following. • Input an integer n. • Create a BST...
Write a program to do the following. • Input an integer n. • Create a BST B containing the same items, but inserting them in an order that will yield the tree balanced. The following algorithm, known as pre-order traversal, gives you a strategy to produce that sequence. seq(low, high, T){    if (low <= high){        mid = (low+high)/2        T.insert(mid)        seq(low, mid-1, T)        seq(mid+1, high, T)    } } • Measure the...
USE C++ for following problem and also use create full program that takes input from user...
USE C++ for following problem and also use create full program that takes input from user also do not use loops only recursion and try to keep program simple A palindrome is any word, phrase, or sentence that reads the same forwards or backwards. Here are some palindromes: Level Civic Pot top A man a plan a canal Panama Write a boolean function that determines if a string argument is a palindrome. The function should return true if the argument...
Create the logic for a rhyming program that takes in five words from a user input...
Create the logic for a rhyming program that takes in five words from a user input and replaces the selected rhyming words with the user's input, then creates and displays the Humpty Dumpty rhyme with the five words from the user input. Hint: 1. You will use the sentinel values: start and stop 2. The program will ask the user for 5 words. 3. The program will store the 5 words 4. The program outputs the rhyme replacing the ____...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT