Hi,
I'm new to this Forum. I need to find the correct answers for following questions.
Exercise: write down PHP code to print all ODD numbers between 0 and 100. a) Using for loop b)Using while loop
Exercise: Define a PHP function to get a positive integer as input as input parameter and to return the sum of all integers from 1 up to that number
Irosh
I generally do not advocate doing other people's homework or helping them get a job/certification when they don't actually have the knowledge. So, have you tried to solve these yourself? We can meet you half way if you do some of the work, and then we can help you if you have a specific problem that is stumping you.
Actually this is not for me. I'm not a programmer (I'm a HR person). This is for my wife's brother. He is working at Innodata Lanka. Tomorrow he has classes he has not done his homework due to following reasons, 1. His personal PC has crashed. He just sent me this through his mobile asking the help. H 2. Due to heavy workload he is working from morning to 10.30pm on these days. Therefore he couldn't go to any cafe to do this. 3. Innodata is a Data processing company and they don't have access to internet (only intranet).
I really appreciate if you could help me. It is 1am here and need to send him through my mobile.
if($num % 2 == 0) { //The number is even }
There's my half.
Okay, so I wasn't going to help, but I figure this should be good for extra credit (and a 5% piece of the resulting salary):
echo implode(', ', range(1, 99, 2));
We don't need no stinkin' loops. 😉
Haha nice!
bradgrafelman;11003891 wrote:And just to satisfy the requirements of the homework assignment: // Using for loop: for($i = 1; $i == 1; $i++) echo implode(', ', range(1, 99, 2)); // Using while loop: while($i = -1 && !@$i++) echo implode(', ', range(1, 99, 2));
And just to satisfy the requirements of the homework assignment:
// Using for loop: for($i = 1; $i == 1; $i++) echo implode(', ', range(1, 99, 2)); // Using while loop: while($i = -1 && !@$i++) echo implode(', ', range(1, 99, 2));
nice.
And since I'm guessing the other question is related to loops, too, we can use a similar approach to Brad's:
function getSumOfIntegers($integer) { while(true) return ($integer * ($integer + 1)) / 2; }
NogDog;11003882 wrote:Okay, so I wasn't going to help, but I figure this should be good for extra credit (and a 5% piece of the resulting salary): echo implode(', ', range(1, 99, 2)); We don't need no stinkin' loops. 😉
Okay, so I wasn't going to help, but I figure this should be good for extra credit (and a 5% piece of the resulting salary):
HI, This answer for which question? complete answer?
NogDog;11003898 wrote:And since I'm guessing the other question is related to loops, too, we can use a similar approach to Brad's: function getSumOfIntegers($integer) { while(true) return ($integer * ($integer + 1)) / 2; }
Is this for the last question? (Define a PHP function to get a positive integer as ....................)
Iroshw;11003904 wrote:Hi, Is this for the last question? (Define a PHP function to get a positive integer as ....................) Irosh
Yes, and the line beginning with "while" is completely superfluous, as we are having a bit of fun finding ways to solve the test questions in ways we do not think the test-giver was intending.
Similarly, my other "answer" gives the requested functionality (odd integers 1 - 99) without actually using any loops.