Computer Science, asked by raginimehta, 11 months ago

write a program in java to find the sum of the series 3-5+8-12+17.......upto n terms.

Answers

Answered by QGP
22

Series Summation Program in Java

We have a series whose sum we have to find by writing a program in Java. Let us analyze the series to understand the pattern.


Consider just the absolute value of the terms:


3, 5, 8, 12, 17, ...

The pattern lies in their difference:


\begin{aligned}\sf 5-3=2 \\\sf 8-5 = 3 \\\sf 12-8=4\\\sf 17-12=5\end{aligned}


As we can see, the difference gets increased by 1 with each term.


So, while writing the program, we must include a variable which will contain the value of Sum, then we should have a variable which would contain the value of each term as we progress through a loop, and finally we should also have a variable by which we would increment the terms. The value of this increment variable would itself increment with each iteration of the loop.


Here is a Java program implementing everything and calculating the Series Sum:


import java.util.Scanner;       //Importing Scanner


public class Brainly    //Creating class

{

   public static void main(String[] args)      

   {

       Scanner sc = new Scanner(System.in);    //Creating Scanner object

       

       System.out.print("Enter number of terms: ");    //Asking for user input

       int n = sc.nextInt();           //Checking user input

       

       int Sum = 0;        //Creating and Initialising Sum to 0

       

       int term = 3;       //The value of the current term

       int incrementValue = 2;     //The increment value by which term is to be incremented

       

       for(int i=0;i<n;i++)    //Setting a loop to run n times

       {

           if(i%2==0)          //If i is even, term is added to Sum

           {

               Sum = Sum + term;

           }

           else                //If i is odd, term is subtracted from Sum

           {  

               Sum = Sum - term;

           }

           

           term = term + incrementValue;       //Incrementing term by required value

           incrementValue++;       //Incrementing this variable for next iteration

       }

       

       System.out.println("Series Sum = "+Sum);    //Printing Sum

       

   }

}

Attachments:

raginimehta: can u pls solve the other ques too..the 2nd one
QGP: Yeah I would try to do it soon :)
raginimehta: yeah pls help me out soon...and thanks a lottt buddy
Answered by achalkumar51
2
this is your answer .please learn the way how to respond to someone,i have seen the way you have responded to adiba
Attachments:

raginimehta: i jst want to tell u RIPur program is absolutely wrong darling....jst do the dry ran and check..u put the condition b<=n and u know the worst part the value of n is not changing....so this program will run infinity...this was very harsh mistake and mny more are there....and about the way i responded i would like like to tell u that ppl should have common sense about how and where to reply to questions.....adiba's answer was noway related to my asked question....
raginimehta: BTW who asked u to provide social service?
raginimehta: sending solution was enough but social service on such sites costs a lot!! so BEWARE pls
Similar questions