The program below is the answer to Deitel’s Java How to Program (9th Edition) Chapter 2 Exercise 2.32.
Click here to see other answers to Java How to Program.
Question: Write a program that inputs five numbers and determines and prints the number of negative numbers input, the number of positive numbers input and the number of zeros input.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.32 - Negative, Positive And Zero Values<br /> * This Program Displays The Number Of Negative, Positive And Zero Values From Five Numbers<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class Ex02_32 {<br /> public static void main (String [] args) {<br /><br /> Scanner value = new Scanner (System.in);<br /><br /> int num1;<br /> int num2;<br /> int num3;<br /> int num4;<br /> int num5; <br /> int numPositive = 0; //variable to count how many positive numbers initialized to zero<br /> int numZero = 0; //variable to count how many numbers are zero initialized to zero<br /> int numNegative = 0; //variable to count how many negative numbers initialized to zero<br /> <br /> System.out.print ("Enter Your First Number: ");<br /> num1 = value.nextInt();<br /> System.out.print ("Enter Your Second Number: ");<br /> num2 = value.nextInt();<br /> System.out.print ("Enter Your Third Number: ");<br /> num3 = value.nextInt();<br /> System.out.print ("Enter Your Fourth Number: ");<br /> num4 = value.nextInt();<br /> System.out.print ("Enter Your Fifth Number: ");<br /> num5 = value.nextInt();<br /> <br /> //count negative numbers<br /> <br /> if (num1 < 0) <br /> numNegative = numNegative + 1;<br /> if (num2 < 0) <br /> numNegative = numNegative + 1;<br /> if (num3 < 0) <br /> numNegative = numNegative + 1;<br /> if (num4 < 0) <br /> numNegative = numNegative + 1;<br /> if (num5 < 0) <br /> numNegative = numNegative + 1;<br /><br /> //count positive numbers<br /> <br /> if (num1 > 0) <br /> numPositive = numPositive + 1;<br /> if (num2 > 0) <br /> numPositive = numPositive + 1;<br /> if (num3 > 0) <br /> numPositive = numPositive + 1;<br /> if (num4 > 0) <br /> numPositive = numPositive + 1;<br /> if (num5 > 0)<br /> numPositive = numPositive + 1;<br /> <br /> //count number of zeros<br /> <br /> if (num1 == 0) <br /> numZero = numZero + 1;<br /> if (num2 == 0) <br /> numZero = numZero + 1;<br /> if (num3 == 0) <br /> numZero = numZero + 1;<br /> if (num4 == 0) <br /> numZero = numZero + 1;<br /> if (num5 == 0) <br /> numZero = numZero + 1;<br /> <br /> System.out.println ("n"); <br /><br /> System.out.printf ("Negative numbers = %dn", numNegative);<br /> System.out.printf ("Positive numbers = %dn", numPositive);<br /> System.out.printf ("Zeros = %dn", numZero);<br /><br /> }<br />}<br /> |
1 2 |