QuestionAsk Questions?

Posted in : Java Beginners

Hi

if someone could help that would be great

Write a program to read in 10 integer values and output the sum of all the positive integers and the sum of all the negative integers.

View Answers

November 14, 2011 at 10:53 AM

import java.util.*; class Numbers { public static void main(String[] args) { Scanner input=new Scanner(System.in); int sum1=0,sum2=0; int num[]=new int[10]; System.out.println("Enter 10 numbers: "); for(int i=0;i<num.length;i++){ num[i]=input.nextInt(); if(num[i]>0){ sum2+=num[i]; } else{ sum1+=num[i]; } } System.out.println("Sum of positive numbers: "+sum2); System.out.println("Sum of negative numbers: "+sum1); } }

Related Tutorials/Questions & Answers: