Program to find sum of series (1+(1+2)+(1+2+3)+...till N)

Problem statement:- Program to find the sum of series (1+(1+2)+(1+2+3)+...till N)

 Data requirement:-

   Input Data:- n

  Output Data:-Sum

  Additional Data:-i, j

Program in C
  
Here is the source code of the C Program to find the sum of series (1+(1+2)+(1+2+3)+...till N).

Code:

#include<stdio.h>
int main()
{
    int n,i=1,j,sum=0;
    printf("Enter the range of number:");
    scanf("%d",&n);
    while(i<=n)
    {

        for(j=1;j<=i;j++){
        sum+=j;
        }
        i++;
    }
    printf("The sum of the series = %d",sum);
}

Input/Output:
Enter the range of number:7
The sum of the series = 84

Program in C++
  
Here is the source code of the C++ Program to find the sum of series (1+(1+2)+(1+2+3)+ ...till N).

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,i=1,j,sum=0;
    cout<<"Enter the range of number:";
    cin>>n;
    while(i<=n)
    {
        for(j=1;j<=i;j++){
        sum+=j;
        }
        i++;
    }
    cout<<"The sum of the series = "<<sum;
}


Input/Output:
Enter the range of number:11
The sum of the series = 286

Program in Java
  
Here is the source code of the Java Program to find the sum of series (1+(1+2)+(1+2+3)+ ...till N).

Code:

import java.util.Scanner;
public class p14 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
    int n,i=1,j,sum=0;
    System.out.println("Enter the range of number:");
    n=cs.nextInt();
    while(i<=n)
    {
        for(j=1;j<=i;j++){
        sum+=j;
        }
        i++;
    }
    System.out.println("The sum of the series = "+sum);
    cs.close();
}

}

Input/Output:
Enter the range of number:
20
The sum of the series = 1540

Program in Python
  
Here is the source code of the Python Program to find the sum of series (1+(1+2)+(1+2+3)+ ...till N).

Code:

print("Enter the range of number:")
n=int(input())
print("Enter the value of x:")
x=int(input())
sum=0
i=1
while(i<=n):
    for j in range(1,i+1):
        sum+=j
    i+=1
print("The sum of the series = ",sum)

Input/Output:
Enter the range of number:
33
The sum of the series =  6545

Post a Comment

0 Comments