Wednesday, August 17, 2011

Sum of elements of one dimensional array


import java.io.*;
class array1sum
{
 public static void main(String[] arg)
  {
   int n ,sum=0;
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
   int arr[]=new int[30];
    try
    {
     System.out.print("Enter the number of elements of array: ");
      n=Integer.parseInt(in.readLine());
     for(int i=0;i<n;i++)
      {
      System.out.print("Enter the value of"+(i+1)+" th elements of array: ");      
        arr[i]=Integer.parseInt(in.readLine());
      }
      for(int i=0;i<n;i++)
      {
       sum+=arr[i];
      }
      System.out.println("Elements of array:");
      for(int i=0;i<n;i++)
      {
       System.out.println(arr[i]);
      }
      System.out.println("Sum of elements of array:"+sum);
     }
    catch(Exception e){ }
   }
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More