mathajax

Java program Sum of Array Integers



The java program find sum of  integers.  
   

Pseudocode of sum of integer 

set sum =0
Read an integer N
initialize integer array size of N

for n= 0  to N-1 
     Read an integer data
     store data into array
end for 
  
 for each E  in  array 
    sum = sum + E
end each

print sum

Java keywords and objects usage in the program

  • BufferedReader class object : provides stream mechanism to read inputs from user/keyboard
  • Integer class object : converts string object into java primitive datatype int
  • for-loop :- repeats reading inputs from user upto N times
  • for-each  : to read element from array in sequential order 




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SumofArrayNumber {

public static void main(String[] args) throws IOException
        {
     
InputStreamReader dis =new InputStreamReader ( System.in );
BufferedReader br =new BufferedReader(dis);

System.out.println("Enter value for N");
       String  arg1 =br.readLine();
 
       int N = Integer.parseInt(arg1);  
       int arrinp[] =new int[N];
 
      for( int n=0;n<N;n++ )
        {    
    System.out.println("Enter " + n +"th value :" );  
    arrinp[n]= Integer.parseInt(br.readLine());
       }
 
      int sum=0;
     for(int ele  : arrinp )
     {          
   sum += ele;
     }
      
   System.out.println("Sum of Values : " + sum);
}

}


Compile and Execution steps

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Binary 1's and 2's Complement