{"id":1394,"date":"2022-03-03T19:20:00","date_gmt":"2022-03-03T19:20:00","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1394"},"modified":"2022-03-03T19:20:00","modified_gmt":"2022-03-03T19:20:00","slug":"program-to-sum-the-elements-of-an-array","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-sum-the-elements-of-an-array\/","title":{"rendered":"Program to sum the elements of an array"},"content":{"rendered":"\n<p>n this tutorial we will see&nbsp;<strong>how to sum up all the elements of an array<\/strong>.<\/p>\n\n\n\n<p>Program 1: No user interaction<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * @author: BeginnersBook.com\n * @description: Get sum of array elements\n *\/\nclass SumOfArray{\n   public static void main(String args&#91;]){\n      int&#91;] array = {10, 20, 30, 40, 50, 10};\n      int sum = 0;\n      \/\/Advanced for loop\n      for( int num : array) {\n          sum = sum+num;\n      }\n      System.out.println(\"Sum of array elements is:\"+sum);\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum of array elements is:160<\/code><\/pre>\n\n\n\n<p>Program 2: User enters the array\u2019s elements<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * @author: BeginnersBook.com\n * @description: User would enter the 10 elements\n * and the program will store them into an array and \n * will display the sum of them.\n *\/\nimport java.util.Scanner;\nclass SumDemo{\n   public static void main(String args&#91;]){\n      Scanner scanner = new Scanner(System.in);\n      int&#91;] array = new int&#91;10];\n      int sum = 0;\n      System.out.println(\"Enter the elements:\");\n      for (int i=0; i&lt;10; i++)\n      {\n    \t  array&#91;i] = scanner.nextInt();\n      }\n      for( int num : array) {\n          sum = sum+num;\n      }\n      System.out.println(\"Sum of array elements is:\"+sum);\n   }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the elements:\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\nSum of array elements is:55<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>n this tutorial we will see&nbsp;how to sum up all the elements of an array. Program 1: No user interaction Output: Program 2: User enters the array\u2019s elements Output:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[404],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1394"}],"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=1394"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1394\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}