Sum of Series | Practice | GeeksforGeeks

Write a program to find the sum of the given series 1+2+3+ . . . . . .(N terms) 
Example 1:
Input:
N = 1
Output: 1
Explanation: For n = 1, sum will be 1.

Example 2:
Input:
N = 5
Output: 15
Explanation: For n = 5, sum will be 1
+ 2 + 3 + 4 + 5 = 15.

Your Task:
Complete the function seriesSum() which takes single integer n, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 105
 
0
vipulmrawal5 hours ago
class Solution{
public:
// function to return sum of  1, 2, ... n
long long seriesSum(int n) {
   
    return (long )n*(long)(n+1)/2;
}
};
0
mohammadravoof5 days ago
c++ easy solution
class Solution{public:// function to return sum of  1, 2, ... nlong long seriesSum(long long n) {    // code here        return n * (n + 1) / 2;}};
0
muskanbhasin19aug2 weeks ago
class Solution{public:// function to return sum of  1, 2, ... nlong long seriesSum(int n) {    // code here   return (long long) n*(n+1)/2;}}; 
0
rohitpandey4842 weeks ago
 
int sum=0;
for(int i=0;i≤no;i++)
{
  sum=sum+i;
}
return sum;
why this is  not working
 
0
maneshram20Premium3 weeks ago
C++ easy solution
class Solution{
public:
	// function to return sum of  1, 2, ... n
	long long seriesSum(int n) {
	    return (long)n*(n+1)/2;
	}
};
0
rumi1w8ry4 weeks ago
def seriesSum(self,n):       return int((n*(n+1))/2)
0
joacrb18l1 month ago
def seriesSum(self,n):    return int(n*(n+1)/2)
+2
ankitsingh2791101 month ago
long seriesSum(int n) {        return (long)n*(n+1)/2;  }}
 
please give vote to support me
 
0
arpitmanuja0002 months ago
 return ((long)n*((long)n+1))/2;
 
without long it will not able to held large values .
 
0
pankajkumarravi2 months ago
one line solution
************************
 return (long)n*(n+1)/2;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.