{"id":1351,"date":"2022-03-03T07:17:30","date_gmt":"2022-03-03T07:17:30","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1351"},"modified":"2022-03-03T07:17:30","modified_gmt":"2022-03-03T07:17:30","slug":"calculate-average-using-arrays","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/calculate-average-using-arrays\/","title":{"rendered":"Calculate Average Using Arrays"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>public class Average {\n\n    public static void main(String&#91;] args) {\n        double&#91;] numArray = { 45.3, 67.5, -45.6, 20.34, 33.0, 45.6 };\n        double sum = 0.0;\n\n        for (double num: numArray) {\n           sum += num;\n        }\n\n        double average = sum \/ numArray.length;\n        System.out.format(\"The average is: %.2f\", average);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The average is: 27.69<\/samp><\/code><\/pre>\n\n\n\n<p>In the above program, the&nbsp;<var>numArray<\/var>&nbsp;stores the floating-point values whose average is to be found.<\/p>\n\n\n\n<p>Then, to calculate the&nbsp;<var>average<\/var>, we need to first calculate the&nbsp;<var>sum<\/var>&nbsp;of all elements in the array. This is done using a for-each loop in Java.<\/p>\n\n\n\n<p>Finally, we calculate the average by the formula:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>average = sum of numbers \/ total count<\/code><\/pre>\n\n\n\n<p>In this case, the total count is given by&nbsp;<code>numArray.length<\/code>.<\/p>\n\n\n\n<p>Finally, we print the average using&nbsp;<code>format()<\/code>&nbsp;function so that we limit the decimal points to only 2 using&nbsp;<code>\"%.2f\"<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Output In the above program, the&nbsp;numArray&nbsp;stores the floating-point values whose average is to be found. Then, to calculate the&nbsp;average, we need to first calculate the&nbsp;sum&nbsp;of all elements in the array. This is done using a for-each loop in Java. Finally, we calculate the average by the formula: In this case, the total count is given [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[403],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1351"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1351"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1351\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}