You are on page 1of 2

Oops practical 2

Q) Write a Java program that reads an unspecified number of integers,


determines how many positive and negative values have been read, and
computes the total and average of the input values (not counting zeros). Your
program ends with the input 0. Display the average as a floating-point number.

180040505 oops s6

MALLIKARJUNA REDDY

Code :-
import java.util.Scanner;
public class lab2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int x=1;
int a=0,b=0,c=0,d=0,sum=0;
System.out.println("enter elements= ");
while(x!=0)
{
x=s.nextInt();
if(x>0)
a=a+1;
else if(x<0)
b=b+1;

sum=sum+x;
d=d+1;

}
float avg=0;
avg=(float) sum/(d-1);
System.out.println("positive numbers = " + a);
System.out.println("negative numbers = " + b);
System.out.println("sum = " + sum);
System.out.println("average = " + avg);
//180040505 g.mallikarjunareddy
}

}
Output :-

enter elements=
1
-2
3
-4
5
-6
7
-8
9
0
positive numbers = 5
negative numbers = 4
sum = 5
average = 0.5555556

You might also like