0% found this document useful (0 votes)
76 views2 pages

Oops Practical 2

The document contains a Java program that reads integers from user input until 0 is entered, counts the positive and negative values, calculates the total and average of the non-zero numbers, and displays the results. It prompts the user to enter elements, initializes variables to track counts and totals, uses a while loop to read inputs and classify them, and after the loop prints the positive count, negative count, total, and average.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

Oops Practical 2

The document contains a Java program that reads integers from user input until 0 is entered, counts the positive and negative values, calculates the total and average of the non-zero numbers, and displays the results. It prompts the user to enter elements, initializes variables to track counts and totals, uses a while loop to read inputs and classify them, and after the loop prints the positive count, negative count, total, and average.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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