Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php Algorithm to print a aa aaa ab aab till zzz

Tags:

loops

php

Hi i need To print from a to zzz upto 3 letters , for example my output should be

A
B
.
.
.
Z
AA
AB
.
.
AZ
BA
BB
.
.
.
ZZ
AAA
AAB
.
.
.
.
ZZZ

I was trying hard for past 5 hours , I cant find any logic and i tried below code

<?php
for ($i=65; $i<=90; $i++) { 
for ($i=65; $i<=90; $i++) {     
for ($i=65; $i<=90; $i++) {     
    echo chr($i).chr($i).chr($i)."<br>";      
}
}
}
?>
like image 258
Anisha Virat Avatar asked Sep 23 '13 19:09

Anisha Virat


1 Answers

PHP has a convenient feature where incrementing a string works exactly as you describe.

So all you need is:

for( $i="A"; $i!="ZZZ"; $i++) {
    echo $i."<br />";
}

EDIT: revised solution that prints 'ZZZ' (instead of 'ZZY') last:

$i = 'A';
do {
  echo $i . '<br />';
} while ( $i++ != 'ZZZ' );
like image 84
Niet the Dark Absol Avatar answered Sep 20 '22 23:09

Niet the Dark Absol