Square Every Number
PHP
Medium
3 views
Problem Description
Given n numbers, use a function to square each and print the squared list.
Input Format
First n. Next line n integers.
Output Format
One line n integers.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0; $n=intval($tokens[$i++] ?? 0);
function sq($x){ return $x*$x; }
$output=[];
for($k=0;$k<$n;$k++) $output[] = strval(sq(intval($tokens[$i++] ?? 0)));
echo implode(' ',$output);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!