Program to Find the sum of series 2+4+6+8.....+N

 Problem statement:- Program to Find the sum of series 2+4+6+8.....+N.

 Data requirement:-

   Input Data:- n

  Output Data:-Sum

  Additional Data:- i

Program in C
  
Here is the source code of the C Program to Find the sum of series 2+4+6+8.....+N.

Code:

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

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

Program in C++

Here is the source code of the C++ Program to Find the sum of series 2+4+6+8.....+N.

Code:

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

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


Program in Java

Here is the source code of the Java Program to Find the sum of series 2+4+6+8.....+N.

Code:

import java.util.Scanner;
public class Sum_Of_Series5 {

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

Input/Output:
Enter the range of number:
6
The sum of the series = 12

Program in Python

Here is the source code of the Python Program to Find the sum of series 2+4+6+8.....+N.

Code:

n=int(input("Enter the range of number:"))
sum=0
i=0
while i<=n:
    sum+=i
    i+=2
print("The sum of the series = ",sum)

Input/Output:
Enter the range of number:12
The sum of the series =  42


Post a Comment

0 Comments