Thursday, 21 January 2021

WAP in Java to read and display the sum of elements in a Single Dimensional array.

 import java.util.*;

class Sum_Array

{

  public static void main(String args[])

  {

  Scanner in=new Scanner(System.in);      

  int N,i,sum=0;

  System.out.println("Enter the size of the array");

  N=in.nextInt();

  int arr[]=new int[N];

  System.out.println("Enter "+N+" Numbers");

  for(i=0;i<N;i++)

  {

  arr[i]=in.nextInt();

  sum=sum+arr[i];

  }

  System.out.println("The Sum of the elements of the array is: "+sum);

  }

}

No comments:

Post a Comment

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...