Computer Science, asked by Rohittiwary, 1 year ago

11.Java programwrite a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd numbers from a list of numbers(N) entered by the user .The list terminates when the user enters the user.
write in the util package.

Answers

Answered by Keshav786
4
import java.util.* ;
class Number
{
public static void main ()
{
Scanner sc =new Scanner ( System.in );
Int n,s,a,b;
s=0;
a=0;
b=0;
S.o.pln(" Enter a number");
n =sc.next Int();
while ( n != 0)
{
if (n<0)
s+=n;
if (n>0&& n%2==0)
a+=n;
if ( n>0&&n%2!=0)
b+=n;
S.o.pln(" Enter next no.(press 0 to end )");
n=sc.next Int();
}
S.o.pln (" sum of negative no.=" +s);
S.o.pln("sum of positive even no. ="+a);
S.o.pln("sum of positive odd no.=" +b);
}
}
Similar questions