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
Answers
Answered by
103
import java.util.*;
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);
}
}
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);
}
}
Answered by
2
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:
Similar questions