Home Jsp Simple-jsp-example Finding a Factorial using while loop
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Finding a Factorial using while loop

Advertisement
In this example we are going to find out the factorial of 12 by using the while loop.

Finding a Factorial using while loop

        

In this example we are going to find out the factorial of 12 by using the while loop. In while loop the loop will run until the condition we have given gets true. We are using the scriptlet to calculate the factorial of 12. In the scriptlet we generally writes a java logic in it. The output will be displayed by using the out implicit object used to write the content on the browser.

We can find a factorial of a number by using the temp variable which will help us to calculate the factorial of a number. Take one variable factorial of int type and initialize it as 1. Take another variable value of type int of which you want to calculate the factorial .Now use while loop and pass the condition that until temp> 0, the loop should perform the following task: the factorial =  factorial* temp and keep on decreasing the value of temp by l.

<HTML>
  <HEAD>
    <TITLE>Finding a Factorial using while loop</TITLE>
  </HEAD>
        <BODY>
    <font  size="6" color ="#000080">Finding a Factorial using while loop</font><br>
    <%
        int value = 12, factorial = 1, temp = value;
              while (temp > 0) {
            factorial *= temp;
            temp--;
        }
              out.println("The factorial of " + value + " is " + factorial + ".");
    %>
  </BODY>
</HTML>     

The output of the program is given below:

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 6, 2008

Ask Questions?    Discuss: Finding a Factorial using while loop   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
dudin21
September 2, 2011
finding HCF using do-while loop

What is the code in finding the highest common factor of two integer?
Sandy Togonon
October 25, 2011
web programing

share ur knowledges.
kannan
June 13, 2012
java programming

You are required to write an application called Customer Billing System. This system will calculate water usage charges for a month that has been imposed by Syarikat Air Melaka Berhad. This system will categorise users into residential users and industrial users and calculate the charges based on data entered by the user. Data needed are the type of customer (Residential or Industrial), past meter reading and current meter reading. Given the charge rate as follows: For Residential: 0 â?? 15 m3 : RM 0.82 every m3 15.1 â?? 40 m3 : RM 0.65 every m3 40.1 m3 and above : RM 0.42 every m3 Minimum charge is RM 5.00 For Industrial: 0 m3 and above : RM 1.47 every m3 Minimum charge is RM15.00 Instruction: The above application must be able to hold information for up to 5 customers. Use an array and repetition structure to store information about users such as type of customer, past meter reading, current meter reading and charges. Write a structured Java program to develop the above system by following the steps below: a) Prompt the user to input the type of customer, past meter reading and current meter reading. Next, get the difference between current meter reading and past meter reading. b) Separate them as either R/r for Residential or I/i for Industrial. You can use any one of the two selection structures. c) Calculate the charges based on rates given above. If there is no water consumption, the minimum charge will be imposed. You can use any one of the two selection structures. d) Display information about the type of customer, past meter reading, current meter reading and charges for each customer. Print the information in table format.
DMCA.com