(pls need help)Want to add in php and ruby (2programs)?

varunrai

New Member
Messages
24
Reaction score
0
Points
0
Input: 12345
= 1 + 2 + 3 + 4 + 5 = 15
= 1 + 5 = 6
Output: 6

I want to get the above mentioned output in php and ruby. can anyone pls help me. im new to php
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
Here is a simple script that I made to do this, it includes a html input box where a user can put a number in. I don't know ruby so this script is in php.

PHP:
<?php 

	// 	If we have been sent a string then lets do stuff with it!
	if(isset($_POST['num_str']))
	{
		//	Store the origional number into the num string.
		$num = $_POST['num_str'];
		
		//	Only repeat the adding process while $num is greater than 1.
		while(strlen($num)>1)
		{
			$temp_num = 0;
			
			//	Go through all of the digits of the number and add them together.
			for($i=0; $i<strlen($num); $i++)
				$temp_num = $temp_num+substr($num, $i , 1);
			
			//	Replace the old number with the new one and then start again!
			$num = $temp_num;
		}
		
		//	Check that the number is not zero.
		if($num != 0)
			echo("The number is... ".$num."<br /><br />");
		//	Otherwise is can't be a numberic value
		else
			echo("Please enter a integer numeric value.<br /><br />");
	}
	//	Otherwise return some 
?>

Please Enter a Number.<br />
<form action="test.php" method="post">
<input type="text" name="num_str">
</form>
 
Last edited:
Top