Septanet Forum  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 25-08-2011, 02:16 PM   #1
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile swaping two numbers

swaping two numbers with the use of third variable
PHP Code:
$num=0;
$num1=10;
$num2=20;
echo 
$num1;
echo
"\n";
echo 
$num2;
$num=$num1;
$num1=$num2;
$num2=$num;
echo
"\n";
echo 
$num1;
echo
"\n";
echo 
$num2
Code:
10
20
20
10
and without using third variable

PHP Code:
$num1=10;
$num2=20;
echo 
$num1;
echo
"\n";
echo 
$num2;
$num1=$num1+$num2;
$num2=$num1-$num2;
$num1=$num1-$num2;

echo
"\n";
echo 
$num1;
echo
"\n";
echo 
$num2
Code:
10
20
20
10
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 26-08-2011, 05:28 PM   #2
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 111
Thumbs up

Very useful.

Though both examples give same result, I prefer using first approach. The second approach requires extra processes for addition and subtraction.

What do you think???
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 29-08-2011, 10:02 AM   #3
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

yah first one is quit good but second one is maths trick!!!!!!!!
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 29-08-2011, 10:39 AM   #4
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 111
Thumbs up

Agree. the second one is the magic of mathematics. I tried with negative numbers and with decimal too. Always works fine.
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 29-08-2011, 04:57 PM   #5
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

It takes a big penalty if you do not enjoy Mathematics
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 30-08-2011, 09:09 AM   #6
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 111
Question

How is the penalty given? Clarify.
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 30-08-2011, 09:41 AM   #7
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

It is a pharse : AGAR MATH SAMAJH AA JAYE TO MAJA NAHI TO MATH HAI EK SAJA
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 30-08-2011, 11:57 AM   #8
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 111
Wink

Okay....
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 30-08-2011, 12:39 PM   #9
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

thanks you have taken such an interest.
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Reply

Tags
swaping two numbers

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 12:56 PM.


Content Relevant URLs by vBSEO 3.5.0 RC2