How to output all three-digit palindrome numbers through PHP program

青灯夜游
Release: 2023-03-12 06:26:01
Original
4077 people have browsed it

In the previous article "How does PHP output all leap years in the 20th century", we used the characteristics of leap years to introduce a general algorithm for judging leap years. This time we will introduce how to use PHP to determine whether a three-digit number is a palindrome number, and how to output all three-digit palindrome numbers. Interested friends can learn about it~

First we need to understand What is a palindrome number?

Palindromes refer to integers that read the same in forward order (from left to right) and reverse order (from right to left), such as (0, 1, 2, 3, 4 , 5, 6, 7, 8, 9, 11, 22, 121, 656, 757, 12321, 5264625, etc.).

So if a three-digit number is given i, how to determine whether the three-digit number is a palindrome?

Thought:

  • We can take out the numbers in the ones, tens, and hundreds digits of this three-digit number (for example, 121) separately , namely g, s, b.

  • Then add g*100, s*10, and the hundreds digit b: g*100 s*10 b; This will get a new Three digits hws.

  • Use the if statement to determine whether i is equal to hws, and you can determine whether the three-digit number i is The palindromes are numbered.

So we can give a method to determine whether a three-digit number is a palindrome

<?php
header("Content-type:text/html;charset=utf-8");
$i=121;
$b= intval($i/100);
$s= ($i/10)%10;
$g= $i%10;
$hws=$g*100+$s*10+$b;
if($i==$g*100+$s*10+$b){
	echo $i."是回文数";
}
else{
	echo $i."不是回文数";
}
?>
Copy after login

The output result is:

121是回文数
Copy after login

Now that we know how to determine whether a three-digit number is a palindrome, let's increase the difficulty: let'soutput all three-digit palindromes!

Analysis: Output all three-digit palindrome numbers, that is, output the number of palindromes within 100~999; therefore we can use a for loop to limit the range

<?php
header("Content-type:text/html;charset=utf-8");
$num=0;
for($i=100;$i<1000;$i++){
	$b= intval($i/100);
	$s= ($i/10)%10;
	$g= $i%10;
	
	$hws=$g*100+$s*10+$b;
	if($i==$g*100+$s*10+$b){
		echo $i." ";
		$num++;
	}
	
}
echo "<br><br>三位回文数共有".$num."个";
?>
Copy after login

The output result is:

How to output all three-digit palindrome numbers through PHP program

It can be seen that we use a counter $num in the loop body of the for loop. After outputting a three-digit palindrome number each time, Increment by 1, so that you can count how many palindromes there are between 100 and 999.

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

The above is the detailed content of How to output all three-digit palindrome numbers through PHP program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template