Septanet Forum  

Go Back   Septanet Forum > Computers, Software, Harware, Information Technology > PHP Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-08-2011, 11:11 AM   #1
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile Making piramid type shape

PHP Code:
$varNum=1;
while(
$varNum<=9) {
    
$varNum1=1;
     for(
$varSpace=1;$varSpace<=20-$varNum;$varSpace++) {
            echo 
' ';  //if you are running this on browser replace this blank space with &nbsp;
        
}
    while(
$varNum1<=$varNum) {
        
// if you want to creat piramid with character then replace the $varNum1 with desired character like '*' but only
        //  single character to be use
        
echo $varNum1.' ';//if you are running this on browser replace this blank space with &nbsp;
       // echo '& ';//if you are running this on browser replace this blank space with &nbsp;
        
$varNum1++;
    }
    echo 
"\n"// replace \n with <br> if using browser for output
    
$varNum++;

output:
Code:
           1
         1 2
        1 2 3
       1 2 3 4
      1 2 3 4 5 
     1 2 3 4 5 6

Last edited by sharma.s.prakash; 08-08-2011 at 12:55 PM.
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 08-08-2011, 12:00 PM   #2
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 110
Thumbs up

One of the most common examples while studying programing. BUT very useful for building program Logic - both structural and looping... always like this.
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 08-08-2011, 12:29 PM   #3
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile yah i m agreed

yah these are the common example. when at the begining of any programming these are vary use full so that any can know how to manipulate loops whats the effect on out put if any changes are done some variation are applied.These are important at the beginning of programming.

can also try this to make diamond shape

PHP Code:
for($n=1;$n<=9;$n++) {
    for(
$s=1;$s<=20-$n;$s++) {
        echo 
" "// "&nbsp";
    
}
    for(
$m=1;$m<=$n;$m++) {
        echo 
$m.' '//echo $m.'&nbsp;';
    
}
    echo 
"\n"//echo "<br>"; 
}
for(
$n=8;$n>=1;$n--) {
    for(
$s=1;$s<=20-$n;$s++) {
        echo 
" "// "&nbsp";
    
}
    for(
$m=1;$m<=$n;$m++) {
        echo 
$m.' '//echo $m.'&nbsp;';
    
}
    echo 
"\n"//echo "<br>"; 

output
Code:
	           1 
                  1 2 
                 1 2 3 
                1 2 3 4 
               1 2 3 4 5 
              1 2 3 4 5 6 
             1 2 3 4 5 6 7 
            1 2 3 4 5 6 7 8 
           1 2 3 4 5 6 7 8 9 
            1 2 3 4 5 6 7 8 
             1 2 3 4 5 6 7 
              1 2 3 4 5 6 
               1 2 3 4 5 
                1 2 3 4 
                 1 2 3 
                  1 2 
                   1

Last edited by sharma.s.prakash; 08-08-2011 at 12:54 PM.
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 08-08-2011, 04:23 PM   #4
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 110
Thumbs up

This is the Magic of Mathematics and Programming.

I would love to see STAR like display also. Is that possible?
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 25-08-2011, 11:23 AM   #5
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

yah it is possible

replace echo $m.' ';
with echo '* '; and see the output;
you will get it
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 25-08-2011, 11:24 AM   #6
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

your mean this or something to display this shape in STAR Shape.
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 08-09-2011, 10:27 AM   #7
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

PHP Code:
for($i=1;$i<=5;$i++) {
    for(
$n=1;$n<=20-$i;$n++) {
        echo 
" ";
    }
    for(
$j=1;$j<=$i;$j++) {
        echo 
"* ";
    }
    echo
"\n";
    
}
for(
$j=1;$j<=3;$j++) {
    for(
$m=1;$m<=5+($j*2);$m++) {
        echo 
" ";
    }
    for(
$i=1;$i<=15-($j*2);$i++) {

        echo 
"* ";
    }

    echo 
"\n";
}
for(
$j=2;$j>=1;$j--) {
    for(
$m=1;$m<=5+($j*2);$m++) {
        echo 
" ";
    }
    for(
$i=1;$i<=15-($j*2);$i++) {

        echo 
"* ";
    }

    echo 
"\n";
}
for(
$i=5;$i>=1;$i--) {
    for(
$n=1;$n<=20-$i;$n++) {
        echo 
" ";
    }
    for(
$j=1;$j<=$i;$j++) {
        echo 
"* ";
    }
    echo
"\n";
    

Code:
                   * 
                  * * 
                 * * * 
                * * * * 
               * * * * * 
       * * * * * * * * * * * * * 
         * * * * * * * * * * * 
           * * * * * * * * * 
         * * * * * * * * * * * 
       * * * * * * * * * * * * * 
               * * * * * 
                * * * * 
                 * * * 
                  * * 
                   *

Last edited by sharma.s.prakash; 08-09-2011 at 10:55 AM.
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 08-09-2011, 10:51 AM   #8
Senior Member
 
sachin's Avatar
 
Join Date: Aug 2011
Posts: 131
Default

Excellant code
very interesting
sachin is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 19-09-2011, 01:18 PM   #9
Member
 
Join Date: Sep 2011
Posts: 42
Default Twinkle Twinkle

Twinkle, twinkle, little star How I wonder what you are Up above the SEPTANET so high Like a diamond in the sky.
Jasbir Kaur Bhatia is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +5.5. The time now is 01:31 AM.


Content Relevant URLs by vBSEO 3.5.0 RC2