Number Of Negative/Positive Elements Of An Array

Number Of Negative/Positive Elements Of An Array


Write a program to display the number of positive and negative elements in a n x n matrix.

import java.io.*; class SumOfPositiveNegative { public static void main(String ar[])throws Exception { BufferedReader ob=new BufferedReader( new InputStreamReader(System.in)); int i,j,sop=0, son=0; int a[][]=new int[3][3]; System.out.println("Enter "+(3*3)+" elements "); for(i=0;i< 3;i++) { for(j=0;j< 3;j++) { a[i][j]=Integer.parseInt(ob.readLine()); } } for(i=0;i< 3;i++) { for(j=0;j< 3;j++) { if(a[i][j] > 0) sop+=a[i][j]; if(a[i][j] < 0) son+=a[i][j]; } } System.out.println("\nSum of positive elements = "+ sop); System.out.println("\nSum of positive elements = "+ son); } }

Have something to say? Log in to comment on this post.

0 comments