Computer Science, asked by ritwik46, 1 year ago

write a program in Java to print sum of negative numbers sum of positive odd number and sum of positive even numbers from a list of numbers entered by the user.the list terminates when the user enters a zero

Answers

Answered by Anonymous
33




import java.util.Scanner;
class example{
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
intnumber,negative=0,positive_even=0,positive_odd=0;
while(true) {
System.out.print("Enter number : "); number=in.nextInt(); if(number>0) { if(number%2==0) { positive_even=positive_even+number; } else { positive_odd=positive_odd+number; } } else if(number<0) {
negative=negative+number;
}
else
{
break;
}
} System.out.println("Sum of negative numbers : "+negative);
System.out.println("Sum of positive even numbers : "+positive_even); System.out.println("Sum of positive odd numbers : "+positive_odd);
}
}

Hope it helps
Mark as brainliest
Answered by sharathdotc
11

Answer:

import java.util.Scanner;

public class SumOfPosAndNegativeElements {

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 int n = 0, posEvenSum = 0, posOddSum = 0, negNumSum = 0;

 for (;;) {

  System.out.println(" Enter any +ve or -ve number [ 0 to exit ]");

  n = input.nextInt();

  if (n == 0) {

   break;

  } else if (n < 0) {

   negNumSum = negNumSum + n;

  } else {

   if (n % 2 == 0)

    posEvenSum = posEvenSum + n;

   else

    posOddSum = posOddSum + n;

  }

 }

 System.out.println("Sum of Negative Numbers : " + negNumSum);

 System.out.println("Sum of Positive Even Numbers :   " + posEvenSum);

 System.out.println("Sum of Positive Odd Numbers : " + posOddSum);

}

}

Explanation:

Similar questions