Sum 1 to N
PHP
Easy
5 views
Problem Description
Given n, print the sum 1+2+...+n.
Input Format
One integer n.
Output Format
One integer sum.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$n=intval($inputText);
echo intdiv($n*($n+1),2);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!