Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

PHP do while loop tutorial with working examples

PHP do while loop tutorial with working examples

PHP do while loop : on reaching the control to do statement, the program proceeds to evaluated the body of the loop first. At the end of the loop, the test-condition in the while statement is evaluated,so it is categorized as post tested loop.The basic format of do while() statement is:
do{
body of the loop
}while(test-conditon);

For practice some example are given below with question. So try to understand them.
Question:-write a program to print “welcome” five times on browser  using do while()
pro1.php
<?php
$i=1;
do
{
echo “welcome”.”<br>”;
$i++;
}while($i<=10);
?>

Question:-write a program to enter a number and check number is palindrome or not.
pro2.php
<?php
if(isset($_POST[‘b1’]))
{
$number=$_POST[‘t1’];
$cp=$number;
$mod=0;
$rev=0;
do{
$mod=$number%10;
$rev=$rev*10+$mod;
$number=(int)($number/10);
}while($number>0);
if($cp==$rev)
$msg=”Palindrome”;
else
$msg=”Not Palindrome”;
}
?>
<form action=”” method=”post”>
Enter Number<input type=”text” name=”t1″><br>
Reverse<input type=”text” value=”<?php echo $rev;?>”><br>
<input type=”submit” name=”b1″ value=”Reverse”>
</form>

Question:-write a program to enter a number and check entered number is Armstrong or Not (153 is a Armstrong number).
pro3.php
<?php
if(isset($_POST[‘b1’]))
{
$number=$_POST[‘t1’];
$cp=$number;
$digit=0;
$cube_plus=0;
do{
$digit=$number%10;
$cube_plus=$cube_plus+($digit*$digit*$digit);
$number=(int)($number/10);
}while($number>0);
if($cube_plus==$cp)
$msg=”Armstrong”;
else
$msg=”Not Armstrong”;
}
?>
<form action=”” method=”post”>
Enter Number<input type=”text” name=”t1″><br>
Status<input type=”text” value=”<?php echo $msg;?>”><br>
<input type=”submit” name=”b1″ value=”Check”>
</form>

Question:-write a program to enter a number and check entered number is prime or not
pro4.php
<?php
if(isset($_POST[‘b1’]))
{
$number=$_POST[‘t1’];
$i=2;
$h=(int)($number/2);
do{
if($number%$i==0)
break;
$i++;
}while($i<=$h);
if($i>$h)
$msg=”Prime”;
else
$msg=”Not Prime”;
}
?>
<form action=”” method=”post”>
Enter Number<input type=”text” name=”t1″><br>
Status<input type=”text” value=”<?php echo $msg;?>”><br>
<input type=”submit” name=”b1″ value=”Check”>
</form>

Second method for prime number check
pro5.php
<?php
if(isset($_POST[‘b1’]))
{
$number=$_POST[‘t1’];
$flag=0;
$i=2;
$h=(int)($number/2);
do{
if($number%$i==0)
{
$flag=1;
break;
}
$i++;
}while($i<=$h);
if($flag==0)
$msg=”Prime”;
else
$msg=”Not Prime”;
}
?>
<form action=”” method=”post”>
Enter Number<input type=”text” name=”t1″><br>
Status<input type=”text” value=”<?php echo $msg;?>”><br>
<input type=”submit” name=”b1″ value=”Check”>
</form>

Question:-write a program to enter a number and calculate the factorial of entered number.
pro6.php
<?php
if(isset($_POST[‘b1’]))
{
$number=$_POST[‘t1’];
$i=1;
$fact=1;
do{
$fact=$fact*$i;
$i++;
}while($i<=$number);
}
?>
<form action=”” method=”post”>
Enter Number(1-16)<input type=”text” name=”t1″><br>
Factorial<input type=”text” value=”<?php echo $fact;?>”><br>
<input type=”submit” name=”b1″ value=”Check”>
</form>
Second method of factorial calcution.
pro7.php
<?php
if(isset($_POST[‘b1’]))
{
$number=$_POST[‘t1’];
$fact=1;
do{
$fact=$fact*$number;
$number–;
}while($number>=1);
}
?>
<form action=”” method=”post”>
Enter Number(1-16)<input type=”text” name=”t1″><br>
Factorial<input type=”text” value=”<?php echo $fact;?>”><br>
<input type=”submit” name=”b1″ value=”Check”>
</form>

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
Do you have any Query / Suggestion