For Loop/PHP

For Loop/PHP

Write a class called math. It is to have one property called num. It also has one method called factorial. This method is to start at 1 and multiply all of the integers to num. If num is 5 then you would multiply 12345. How would you write this as a loop? Would I need to use the for loop or do/while loop?

View Answers

September 27, 2012 at 11:28 AM

PHP Factorial Example:

<?php 
$n = 5;
$i = 1;
$f = 1;
while($i<$n){
$i = $i + 1;
$f = $f * $i;
}
echo $f; 
?>

September 27, 2012 at 11:29 AM

Here is a java example that finds the factorial of a number.

public class math{
public static long factorial(int n){
if(n <= 1) 
return 1;
else
return n * factorial(n - 1);
}
public static void main(String [] args){
    int num=5;
System.out.println(factorial(num));
}
}









Related Tutorials/Questions & Answers:
for( ) loop in php
for( ) loop in php  Generally, when we use for loop in PHP or any other programming language why we use i++ and not ++i. What is the reason behind
for( ) loop in php
for( ) loop in php  Generally, when we use for loop in PHP or any other programming language why we use i++ and not ++i. What is the reason behind
Advertisements
foreach loop in php
foreach loop in php  Foreach loop in php
How to call Array Length for loop in PHP
How to call Array Length for loop in PHP  Hello, I am learner in PHP scripting Language. How can I use the array length for loop in PHP programming language? Kindly provides online help guide or reference links. Thanks
For Loop/PHP
12345. How would you write this as a loop? Would I need to use the for loop or do/while loop?   PHP Factorial Example: <?php $n = 5; $i = 1; $f...For Loop/PHP  Write a class called math. It is to have one property
foreach Loop
foreach Loop In PHP, for & foreach loop executes code block a specific number of times, or until condition fails. The for Loop The for loop executes... to loop. Given below the example : <html> <body> <?php
PHP FORM Part-8
when the condition is false. The syntax for a while loop in PHP...Topic : HTML FORM While Loop Part - 8 In this part of tutorial, we will learn about the While loop. Instead of using for loop, we can also use while loop
php array foreach/how to use foreach loop to iterate and print all the values in an array
Foreach is enhanced version of the for loop. It is used to iterate through the array elements. It is used only with the array. Example of PHP Array Foreach Loop <?php $ar1=array("mango","apple","orange","grapes
while & do..while Loop
while & do..while Loop while loop The while loop iterate until provided...; <?php $j=1; while($j<=3) { echo "Value of j" . $j . "<... loop always executes its block of code at least once. Given below the syntax
php array loop count
php array loop count  Count the array using loop in php
PHP While Loop
While Control Structure: While is the simplest form of loop. While loop checks..., and become infinite loop. While loop is also known as pre-test loop. Basic format of while loop is as follows: ADS_TO_REPLACE_1 while(condition
php do while syntax
php do while syntax  How to create a do while loop in php. What is the syntax
php do while example
php do while example  Simple example of do while loop in PHP
php do while loop
php do while loop  Difference between do while loop and simple loop in PHP
php array loop key value
php array loop key value  How to use Array key value loop in PHP
php while loop
php while loop  Example to fetch the data using while loop in PHP
PHP Regular Expression
PHP GET & POST PHP Loop PHP Arrays PHP Strings...PHP regular expression allows programmer to perform various task like... strings. PHP Regular expression also allows you to validate a email, phone
PHP While Loop
While Loop in PHP: In programming language we need to use loops for executing... started: While Loop in PHP: While loop checks the condition first... known as pre-test loop.     ADS_TO_REPLACE_2 PHP While Loop
PHP Do While Loop
Do-While Loop Function PHP: PHP Do-While loop method is another type of loop which runs at least once, since this loop checks the condition after executing... of the variable within the loop. PHP do while loop Example:ADS_TO_REPLACE_1 <?php
PHP For Each Loop Function
Foreach Loop in PHP In PHP  associative array gives us more power to use.... To fetch values from associative array we need to use for each loop. Using  for each loop we can assign values of one by one to a variable
PHP For Each Loop
Foreach Loop in PHP In PHP  associative array gives us more power to use... loop. Using  for each loop we can assign values of one by one to a variable and that variable is used inside the loop for various purpose like
PHP Addition of Array
example we have used foreach loop, foreach loop is a well known loop construct and almost every modern language supports foreach loop. PHP 4 has included foreach...PHP Addition an Array to Another Array In this PHP beginners tutorial we

Ads