{"id":2008,"date":"2017-05-14T05:55:00","date_gmt":"2017-05-14T05:55:00","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2008"},"modified":"2024-11-16T14:56:32","modified_gmt":"2024-11-16T14:56:32","slug":"write-a-java-program-to-compute-sum-and-average-of-given-numbers","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-compute-sum-and-average-of-given-numbers\/","title":{"rendered":"Program to compute Sum and Average"},"content":{"rendered":"<h1>Overview<\/h1>\r\n<p>In this post, we will write a simple java program to calculate the sum and average of the given numbers. We will use an integer array to hold the given numbers by the user.<\/p>\r\n<h2>IPO Chart<\/h2>\r\n<h3>Input<\/h3>\r\n<p>Input to the program is the given set of numbers by the user.\u00a0<\/p>\r\n<h3>Process<\/h3>\r\n<p>The mathematical formulae are as follows:<\/p>\r\n<p>Let n be the given set of numbers.\u00a0<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Sum<\/strong> = <img decoding=\"async\" src=\"https:\/\/latex.codecogs.com\/gif.latex?\\LARGE&amp;space;\\sum_{i=0}^{n-1}&amp;space;a[i]\" alt=\"\\LARGE \\sum_{i=0}^{n-1} a[i]\" align=\"absmiddle\" title=\"\"><\/p>\r\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Average =\u00a0 <img decoding=\"async\" src=\"https:\/\/latex.codecogs.com\/gif.latex?\\LARGE&amp;space;\\frac{&amp;space;sum}{&amp;space;n}\" alt=\"\\LARGE \\frac{ sum}{ n}\" align=\"absmiddle\" title=\"\"><\/strong><\/p>\r\n<h3>Output<\/h3>\r\n<p>Sum and Average of the numbers.<\/p>\r\n<h3>Tools and Environment<\/h3>\r\n<ul>\r\n<li>Eclipse IDE<\/li>\r\n<li>JDK<\/li>\r\n<li>Windows 10 OS<\/li>\r\n<\/ul>\r\n<h2>Eclipse IDE Setup<\/h2>\r\n<p>Some steps and instructions to create a Java application on Eclipse IDE:<\/p>\r\n<ul>\r\n<li>Create Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/create-a-new-java-project-in-eclipse\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Package in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/\">https:\/\/www.testingdocs.com\/create-java-package-in-eclipse-ide\/<\/a><\/p>\r\n<ul>\r\n<li>Create Java Class in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/\">https:\/\/www.testingdocs.com\/create-a-new-java-class-in-a-project\/<\/a><\/p>\r\n<ul>\r\n<li>Run Java Project in Eclipse<\/li>\r\n<\/ul>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/\">https:\/\/www.testingdocs.com\/run-java-project-in-eclipse\/<\/a><\/p>\r\n<h2>Java Program<\/h2>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Scanner;\r\n\r\n\/***********************************************************\r\n * SumOfArrayElements.java\r\n * @program : Java program to calculate sum of array elements\r\n * @web : www.TestingDocs.com\r\n * @author :\r\n * @version : 1.0\r\n ************************************************************\/ \r\npublic class SumOfArrayElements {\r\n  public static void main(String args[]) {\r\n    int n=0;\r\n    int sumOfNumbers=0;\r\n    double averageOfNumbers=0.0;\r\n    Scanner sc = new Scanner(System.in);\r\n    \/\/Input of array elements from user.\r\n    System.out.println(\"Enter number n:\");\r\n    n = sc.nextInt();\r\n    int[] array= new int[n];\r\n    for(int i=0;i&lt;n;i++)\r\n    {\r\n      System.out.println(\"Enter number:\" + (i + 1));\r\n      array[i] = sc.nextInt();\r\n      sumOfNumbers = sumOfNumbers + array[i];\r\n    }\r\n\r\n    averageOfNumbers = sumOfNumbers\/ n;\r\n    System.out.println(\"--------- Output of the Program --------------\");\r\n    System.out.println(\"Sum = \" + sumOfNumbers);\r\n    System.out.println(\"Average of Array elements=\" + averageOfNumbers);\r\n  }\r\n}\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<h3>Program Output<\/h3>\r\n<p>Save the program in the IDE. Run the program.<\/p>\r\n<p>Right click and choose <strong>Run As &gt;&gt; Java Application<\/strong><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>Enter number n:<br \/>5<br \/>Enter number:1<br \/>33<br \/>Enter number:2<br \/>34<br \/>Enter number:3<br \/>35<br \/>Enter number:4<br \/>36<br \/>Enter number:5<br \/>37<br \/>&#8212;&#8212;&#8212; Output of the Program &#8212;&#8212;&#8212;&#8212;&#8211;<br \/>Sum = 175<br \/>Average of Array elements=35.0<\/p>\r\n<h3>Screenshot<\/h3>\r\n<p>Screenshot of the program.<\/p>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2059 size-full\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-and-Average-of-Numbers.png\" alt=\"Sum and Average Java Program\" width=\"1739\" height=\"1032\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-and-Average-of-Numbers.png 1739w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-and-Average-of-Numbers-300x178.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-and-Average-of-Numbers-1024x608.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-and-Average-of-Numbers-768x456.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Sum-and-Average-of-Numbers-1536x912.png 1536w\" sizes=\"auto, (max-width: 1739px) 100vw, 1739px\" \/><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>&#8212;<\/p>\r\n<p>Java Tutorial on this website:<\/p>\r\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\r\n<p><br \/>For more information on Java, visit the official website :<\/p>\r\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\r\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2008\" data-postid=\"2008\" class=\"themify_builder_content themify_builder_content-2008 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview In this post, we will write a simple java program to calculate the sum and average of the given numbers. We will use an integer array to hold the given numbers by the user. IPO Chart Input Input to the program is the given set of numbers by the user.\u00a0 Process The mathematical formulae [&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":[],"class_list":["post-2008","post","type-post","status-publish","format-standard","hentry","category-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\/2008","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=2008"}],"version-history":[{"count":10,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2008\/revisions"}],"predecessor-version":[{"id":21769,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2008\/revisions\/21769"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}