write a java program to input any 50 numbers including positive and negative. perform the following tasks:
a.Count positive numbers
b.count negative numbers
c. sum of positive numbers
d.sum of negative numbers
Share
write a java program to input any 50 numbers including positive and negative. perform the following tasks:
a.Count positive numbers
b.count negative numbers
c. sum of positive numbers
d.sum of negative numbers
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
class num
{
public static void main()
{
Scanner in=new Scanner(System.in);
int n,cp=0,cn=0,sp=0,sn=0;
for(int i=1;i<=50;i++)
{
System.out.println("Enter a number");
n=in.nextInt();
if(n>=0)
{
cp++;
sp+=n;
}
else
{
cn++;
sn+=n;
}
}
System.out.println("Number of positive numbers:"+cp);
System.out.println("Sum of positive numbers:"+sp);
System.out.println("Number of negative numbers:"+cn);
System.out.println("Sum of negative numbers:"+sn);
}
}
Answer:
import java.util.*;
import java.io.*;
public class Program {
public static void main(String []args) throws IOException{
int n,cp=0,cn=0,sp=0,sn=0;
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
for(int i=1;i<=50;i++)
{
System.out.println("Enter a number");
n=Integer.parseInt(in.readLine());
if(n>=0)
{
cp++;
sp+=n;
}
else
{
cn++;
sn+=n;
}
}
System.out.println("Number of positive numbers:"+cp);
System.out.println("Sum of positive numbers:"+sp);
System.out.println("Number of negative numbers:"+cn);
System.out.println("Sum of negative numbers:"+sn);
}}
Explanation: