Program to find the sum of series 1+3+5+7..+N

Problem statement:- Program to find the sum of series 1+3+5+7..+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 1+3+5+7..+N.

Code:
  
#include<stdio.h>
int main()
{
    int n,sum=0,i=1;
    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 = 25

Program in C++
  
Here is the source code of the C++ Program to find the sum of series 1+3+5+7..+N.

Code:
  
#include<iostream>
using namespace std;
int main()
{
    int n,sum=0,i=1;
    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:13
The sum of the series = 49

Program in Java
  
Here is the source code of the Java Program to find the sum of series 1+3+5+7..+N.

Code:

import java.util.Scanner;
public class p2 {

public static void main(String[] args) {

Scanner cs=new Scanner(System.in);
    int n,sum=0,i=1;
    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:
27
The sum of the series = 196

Program in Python
  
Here is the source code of the Python Program to find the sum of series 1+3+5+7..+N.

Code:


print("Enter the range of number:")

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

Input/Output:
Enter the range of number:
51
The sum of the series =  676






Post a Comment

0 Comments