Advertisement
mhrabbi

Sum of all elements in array (java)

Feb 12th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SumOfArrElle {
  4.    
  5.     public static void main(String args[])
  6.     {
  7.         Scanner scan=new Scanner(System.in);
  8.        
  9.         int size,sum=0;
  10.        
  11.         System.out.println("Enter the Size of array: ");
  12.         size=scan.nextInt();
  13.        
  14.        
  15.         int [] arr =new int[size];
  16.        
  17.        
  18.         System.out.println("Enter elements in array");
  19.        
  20.         for(int i=0; i<arr.length; i++)
  21.         {
  22.           arr[i]=scan.nextInt();
  23.         }
  24.        
  25.         for(int num : arr)
  26.         {
  27.             sum = sum+num;
  28.         }
  29.        
  30.         System.out.println("Sum of array elements is:"+sum);
  31.         scan.close();
  32.        
  33.        
  34.     }
  35.    
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement