{"id":1912,"date":"2018-04-20T05:19:00","date_gmt":"2018-04-20T05:19:00","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=1912"},"modified":"2024-08-09T09:13:08","modified_gmt":"2024-08-09T09:13:08","slug":"java-program-to-calculate-the-sum-of-series","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-program-to-calculate-the-sum-of-series\/","title":{"rendered":"Java program to calculate the sum of series"},"content":{"rendered":"<h2>Problem Statement:<\/h2>\r\n<p>Write a Java program to calculate the sum of the following series:<\/p>\r\n<p>S = 3 &#8211; 6 + 9 &#8211; 12 + 15 &#8211; 18 &#8230;. &#8211; 30<\/p>\r\n<h3>Flowchart:<\/h3>\r\n<p>Raptor flow chart for the Java program is shown below:<\/p>\r\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1916\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-Sum-FlowChart.png\" alt=\"\" width=\"1920\" height=\"1031\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-Sum-FlowChart.png 1920w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-Sum-FlowChart-300x161.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-Sum-FlowChart-1024x550.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-Sum-FlowChart-768x412.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-Sum-FlowChart-1536x825.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/h3>\r\n<h3>Java Program:<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package series;\r\n\r\n\/*****************************************************\r\n * Java Program:\r\n * To calculate the series sum shown below\r\n * sum = 3 - 6 + 9 - 12 + .... - 30 \r\n ******************************************************\/\r\n\r\npublic class SeriesSum {\r\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  \/\/main method\r\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n  public static void main(String[] args) {\r\n    \/\/ variables declaration\r\n    int sum = 0;\r\n    boolean minusSwitch = false;\r\n    int term=0;\r\n    int endTerm=30;\r\n    \/\/ for loop \r\n    for(int i=3; i&lt;=endTerm; i=i+3) {\r\n      if(minusSwitch)\r\n        term=-i;\r\n      else\r\n        term=i;\r\n      \/\/accumulating the sum\r\n      sum += term;\r\n      \/\/toggle the switch\r\n      minusSwitch  = !minusSwitch ;\r\n    } \/\/ end loop\r\n    System.out.print(\"Sum of Series S = \" + sum);\r\n  } \/\/ end main\r\n} \/\/ end class\r\n\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<h3>Screenshot:<\/h3>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1914\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-SUm-Java-Program.png\" alt=\"\" width=\"1919\" height=\"1005\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-SUm-Java-Program.png 1919w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-SUm-Java-Program-300x157.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-SUm-Java-Program-1024x536.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-SUm-Java-Program-768x402.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Series-SUm-Java-Program-1536x804.png 1536w\" sizes=\"auto, (max-width: 1919px) 100vw, 1919px\" \/><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: Write a Java program to calculate the sum of the following series: S = 3 &#8211; 6 + 9 &#8211; 12 + 15 &#8211; 18 &#8230;. &#8211; 30 Flowchart: Raptor flow chart for the Java program is shown below: Java Program: package series; \/***************************************************** * Java Program: * To calculate the series sum [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[24],"class_list":["post-1912","post","type-post","status-publish","format-standard","hentry","category-java-programs","tag-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1912"}],"version-history":[{"count":5,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1912\/revisions"}],"predecessor-version":[{"id":23870,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1912\/revisions\/23870"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}