Write a program to print the sum of negative numbers, sum of positive odd numbers
and sum of positive even numbers from a list of numbers entered by the user.
The list terminates when the user enters zero.
Answers
Answer: Okay so the follow the program code given below
Explanation:
import java.util.Scanner;
class sum
{
Scanner sc = new Scanner(System.in);
void main()
{
System.out.println(" Enter say numbers as many as you like but if you enter zero the computer will accept numbers no more ");
int sn = 0 , spo = 0 , spe = 0;
for(int i = 1; i ++)
{
int n = sc.nextInt();
if(n < 0)
sn = sn + n;
if(n > 0 && n % 2 != 0)
spo = spo + n;
if(n > 0 && n % 2 == 0)
spe = spe + n;
if( n == 0)
break;
}
System.out.println(" The sum of positive odd numbers is " + spo);
System.out.println(" The sum of negative numbers is " + sn);
System.out.println(" The sum of positive even numbers is " + spe);
}
}
note that the following code is for java programming language
hope it helps
Answer:
JAVA:-
import java.util.*;
public class Question67
{
static void main()
{
int N,sn=0,spe=0,spo=0;
Scanner sc=new Scanner(System.in);
do
{
System.out.println("Enter the value of N:");
N=sc.nextInt();
if(N<0)
sn+=N;
else if(N>0)
{
if(N%2==0)
spe+=N;
else
spo+=N;
}
}while(N!=0);
System.out.println("Sum of negative numbers="+sn);
System.out.println("Sum of positive even numbers="+spe);
System.out.println("Sum of positive odd numbers="+spo);
}
}