- Java Basic Programs
- Java Programming Examples
- Java Print Hello World
- Java Get Input from User
- Java Print Integer
- Java Add two Numbers
- Java Check Even or Odd
- Java Check Prime or Not
- Java Check Alphabet or Not
- Java Check Vowel or Not
- Check Reverse equal Original
- Java Fahrenheit to Celsius
- Java Celsius to Fahrenheit
- Java Perfect Number Program
- Java Find Quotient Remainder
- Java Days to Seconds
- Java Count Digits in Number
- Java Binary Number Addition
- Java Discount Program
- Java Compute Courier Charge
- Java Find Telephone Bill
- Java Print ASCII Values
- Java Check Palindrome or Not
- Java Check Armstrong or Not
- Generate Armstrong Numbers
- Add two Numbers using Pointers
- Java Mathematical Programs
- Add Subtract Multiply & Divide
- Java Make Calculator
- Java Add Digits of Number
- Java Check Leap Year or Not
- Java Check Divisibility
- Java Find Simple Interest
- Java Find Compound Interest
- Java Print Fibonacci Series
- Java Find nCr nPr
- Calculate Average & Percentage
- Java Calculate Arithmetic Mean
- Java Calculate Student Grade
- Java Print Table of Number
- Java Print Prime Numbers
- Java Add n Numbers
- Java Interchange two Numbers
- Java Reverse Numbers
- Java Swap two Numbers
- Count Positive Negative & Zero
- Find Largest of two Numbers
- Find Largest of three Numbers
- Java Find Factorial of Number
- Java Find HCF & LCM
- Area & Perimeter of Square
- Area & Perimeter of Rectangle
- Area & Circumference of Circle
- Java Conversion Programs
- Java Decimal to Binary
- Java Decimal to Octal
- Java Decimal to Hexadecimal
- Java Binary to Decimal
- Java Binary to Octal
- Java Binary to Hexadecimal
- Java Octal to Decimal
- Java Octal to Binary
- Java Octal to Hexadecimal
- Java Hexadecimal to Decimal
- Java Hexadecimal to Binary
- Java Hexadecimal to Octal
- Java Pattern Programs
- Java Pattern of Stars
- Java Pattern of Alphabets
- Java Pattern of Numbers
- Java Pyramid of Stars
- Java Pyramid of Alphabets
- Java Pyramid of Numbers
- Java Print Diamond Pattern
- Java Print Floyd Triangle
- Java Print Pascal Triangle
- Java Array Programs
- One Dimensional Array Program
- Java Linear Search
- Java Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Java Reverse Array
- Insert Element in Array
- Delete Element from Array
- Java Merge two Array
- Java Bubble Sort
- Java Selection Sort
- Java Insertion Sort
- Java Find Common Elements
- Java Count Even/Odd Number
- Two Dimensional Array Program
- Java Add two Matrices
- Java Subtract two Matrices
- Java Transpose Matrix
- Multiply two Matrices
- Three Dimension Array Program
- Java String Programs
- Java Print String
- Find Length of String
- Java Compare two String
- Java Copy String
- Java Concatenate String
- Java Reverse String
- Delete Vowels from String
- Delete Words from Sentence
- Find Occurrence of a Character
- Java Find Occurrence of a Word
- Occurrence of Each Character
- Java Occurrence of Each Word
- Java Count Repeated Characters
- Java Count Repeated Words
- Java Capitalize Each Word
- Java Count Vowels/Consonants
- Java Extract Numbers
- Java Count Word in String
- Remove Spaces from String
- Java Sort a String
- Java Uppercase to Lowercase
- Java Lowercase to Uppercase
- Java Swap two Strings
- Java Check Anagram or Not
- Java Check Balance Parentheses
- Java Check Password Strength
- Java File Programs
- Java Read File
- Java Write to File
- Read & Display File Content
- Java Copy File
- Java Append Text to File
- Java Merge two File
- List files in Directory
- Java Delete File
- Java Miscellaneous Programs
- Generate Random Numbers
- Java Print Time & Date
- Java Get IP Address
- Java Shutdown Computer
- Java Programming Tutorial
- Java Tutorial
Java Program to Count Positive, Zero and Negative Numbers
This article is created to cover a program in Java that counts positive, zero, and negative numbers from a given set of numbers by user at run-time.
Count Positive, Negative and Zero in Java
To count the number of positive and negative numbers along with zero from a given set of numbers entered by user, you have to first receive some set of numbers say 10 numbers. Then check all the number using for loop one by one, to count the number of positive, zero, and negative number(s) available in the given set of 10 numbers and display the output on the screen as shown in the following program.
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int countP=0, countN=0, countZ=0, i; int[] arr = new int[10]; Scanner scan = new Scanner(System.in); System.out.print("Enter 10 Numbers: "); for(i=0; i<10; i++) arr[i] = scan.nextInt(); for(i=0; i<10; i++) { if(arr[i]<0) countN++; else if(arr[i]>0) countP++; else countZ++; } System.out.println("\nTotal Positive Number: " +countP); System.out.println("Total Negative Number: " +countN); System.out.println("Total Zero: " +countZ); } }
When the above Java Program is compiled and executed, it will produce the following initial output:
Now supply any 10 numbers. Here is its sample run with user input 32, 43, 0, -43, -54, -65, 23, 0, 53, 13
The above program can also be created as:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int num, countP=0, countN=0, countZ=0; Scanner scan = new Scanner(System.in); System.out.print("Enter 10 Numbers: "); for(int i=0; i<10; i++) { num = scan.nextInt(); if(num<0) countN++; else if(num>0) countP++; else countZ++; } System.out.println("\nTotal Positive Number: " +countP); System.out.println("Total Negative Number: " +countN); System.out.println("Total Zero: " +countZ); } }
Count Positive and Negative Numbers in Java using while Loop
This program is similar to previous one. But it is created using while loop. Also the program allows user to define the size or the amount of numbers to enter.
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int tot, num, i=0, countP=0, countN=0; Scanner scan = new Scanner(System.in); System.out.print("Enter the Size: "); tot = scan.nextInt(); System.out.print("Enter " +tot+ " Numbers: "); while(i<tot) { num = scan.nextInt(); if(num<0) countN++; else if(num>0) countP++; i++; } System.out.println("\nTotal Positive Number: " +countP); System.out.println("Total Negative Number: " +countN); } }
Count Positive and Negative Numbers using an Array
The question is, write a Java program to count total number of positive and negative numbers available in an array of n numbers. The answer to this question, is the program given below:
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int countP=0, countN=0; Scanner scan = new Scanner(System.in); System.out.print("Enter the Size: "); int tot = scan.nextInt(); int[] nums = new int[tot]; System.out.print("Enter " +tot+ " Numbers: "); for(int i=0; i<tot; i++) { nums[i] = scan.nextInt(); if(nums[i]<0) countN++; else if(nums[i]>0) countP++; } System.out.println("\nTotal Positive Number: " +countP); System.out.println("Total Negative Number: " +countN); } }
Mega Program on Counting Positive, Negative and Zero in Java
This is the last program of this article. I've called this program as the mega program of this article, because it includes all the options.
import java.util.Scanner; public class fresherearth { public static void main(String[] args) { int num, countP=0, countN=0, countZ=0; Scanner scan = new Scanner(System.in); System.out.println("1. Count Positive Number Only"); System.out.println("2. Count Negative Number Only"); System.out.println("3. Count Zero Only"); System.out.println("4. Count Positive and Negative Number"); System.out.println("5. Count Positive Number and Zero"); System.out.println("6. Count Negative Number and Zero"); System.out.println("7. Count Positive, Zero and Negative Number"); System.out.print("Enter Your Choice (1-7): "); int choice = scan.nextInt(); if(choice>=1 && choice<=7) { System.out.print("\nEnter the Size: "); int tot = scan.nextInt(); System.out.print("Enter " +tot+ " Numbers: "); if(choice==1) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num>0) countP++; } } else if(choice==2) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num<0) countN++; } } else if(choice==3) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num==0) countZ++; } } else if(choice==4) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num>0) countP++; else if(num<0) countN++; } } else if(choice==5) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num>0) countP++; else if(num==0) countZ++; } } else if(choice==6) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num<0) countN++; else if(num==0) countZ++; } } else if(choice==7) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num>0) countP++; else if(num<0) countN++; else countZ++; } } if(countP!=0) { if(countP==1) System.out.println("\nThere is only 1 Positive Number Found."); else System.out.println("\nThere are " +countP+ " Positive Numbers Found."); } if(countN!=0) { if(countN==1) System.out.println("\nThere is only 1 Negative Number Found."); else System.out.println("\nThere are " +countN+ " Negative Numbers Found."); } if(countZ!=0) { if(countZ==1) System.out.println("\nThere is only 1 Zero found."); else System.out.println("\nThere are " +countZ+ " Zero Found."); } } else System.out.println("\nInvalid Input!"); } }
Here is its sample run with user input 6 as choice to count negative number and zero only, 6 as size to count based on 6 numbers, 1, 2, -4, -45, 0, 23 as six numbers:
Same Program in Other Languages
« Previous Program Next Program »