Array-promo

Question

Print sum of all elements in an array.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class sumOfElementInAnArray
{  
    public static void main(String[] args)
    {  
        /* Initialize array */  
        int [] arr1;
        
        int n=0,i=0,sum=0;
        Scanner sc = new Scanner(System.in);    
        
        System.out.println("Enter a no. of elements in the array");   
        n = sc.nextInt(); 
        System.out.println("Enter numbers in array");
        arr1=new int[n];
        for(i=0;i< n;i++)
        {
            arr1[i]=sc.nextInt();
        }
        
          
        System.out.println();  
          
        
        for ( i = 0; i < n; i++) 
        {   
            sum=sum+arr1[i];
        }  
        System.out.println("Sum of all element in the array is "+sum); 
    }  
}    
				
			

Coding Store

10-minute Stress-Buster Games

Leave a Reply

Your email address will not be published. Required fields are marked *