Java Program That Reads a Series of Positive Numbers and Counts the Number of Inputs
Coffee Plan to Count Positive, Zero and Negative Numbers
This article is created to encompass a programme in Java that counts positive, zero, and negative numbers from a given set of numbers by user at run-fourth dimension.
Count Positive, Negative and Zero in Coffee
To count the number of positive and negative numbers forth with zero from a given set up of numbers entered past user, yous 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 every bit shown in the post-obit programme.
import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { int countP=0, countN=0, countZ=0, i; int[] arr = new int[10]; Scanner scan = new Scanner(Arrangement.in); Organisation.out.impress("Enter 10 Numbers: "); for(i=0; i<ten; i++) arr[i] = scan.nextInt(); for(i=0; i<ten; i++) { if(arr[i]<0) countN++; else if(arr[i]>0) countP++; else countZ++; } System.out.println("\nTotal Positive Number: " +countP); System.out.println("Full Negative Number: " +countN); System.out.println("Full Zero: " +countZ); } }
When the above Java Program is compiled and executed, information technology will produce the following initial output:
Now supply whatever ten numbers. Here is its sample run with user input 32, 43, 0, -43, -54, -65, 23, 0, 53, 13
The above programme can also be created every bit:
import java.util.Scanner; public class CodesCracker { public static void primary(Cord[] args) { int num, countP=0, countN=0, countZ=0; Scanner scan = new Scanner(Organization.in); System.out.print("Enter 10 Numbers: "); for(int i=0; i<10; i++) { num = browse.nextInt(); if(num<0) countN++; else if(num>0) countP++; else countZ++; } System.out.println("\due northFull Positive Number: " +countP); Organization.out.println("Full Negative Number: " +countN); System.out.println("Full Zero: " +countZ); } }
Count Positive and Negative Numbers in Java using while Loop
This program is like to previous one. But information technology is created using while loop. Too the program allows user to define the size or the corporeality of numbers to enter.
import coffee.util.Scanner; public class CodesCracker { public static void main(String[] args) { int tot, num, i=0, countP=0, countN=0; Scanner browse = new Scanner(Organisation.in); System.out.impress("Enter the Size: "); tot = scan.nextInt(); Organization.out.print("Enter " +tot+ " Numbers: "); while(i<tot) { num = scan.nextInt(); if(num<0) countN++; else if(num>0) countP++; i++; } Organization.out.println("\northTotal 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 full number of positive and negative numbers available in an array of n numbers. The answer to this question, is the plan given beneath:
import coffee.util.Scanner; public class CodesCracker { public static void principal(Cord[] args) { int countP=0, countN=0; Scanner scan = new Scanner(Organisation.in); System.out.impress("Enter the Size: "); int tot = scan.nextInt(); int[] nums = new int[tot]; System.out.impress("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("\due northFull Positive Number: " +countP); System.out.println("Total Negative Number: " +countN); } }
Mega Program on Counting Positive, Negative and Nix in Coffee
This is the concluding program of this commodity. I've called this program as the mega plan of this article, because it includes all the options.
import java.util.Scanner; public class CodesCracker { public static void chief(String[] args) { int num, countP=0, countN=0, countZ=0; Scanner scan = new Scanner(Organization.in); Arrangement.out.println("1. Count Positive Number But"); System.out.println("two. Count Negative Number Only"); System.out.println("three. Count Zero Simply"); Arrangement.out.println("iv. Count Positive and Negative Number"); System.out.println("5. Count Positive Number and Zilch"); System.out.println("half dozen. Count Negative Number and Zero"); System.out.println("seven. Count Positive, Zero and Negative Number"); System.out.print("Enter Your Choice (i-7): "); int selection = scan.nextInt(); if(option>=1 && choice<=7) { Organization.out.print("\nEnter the Size: "); int tot = browse.nextInt(); System.out.print("Enter " +tot+ " Numbers: "); if(choice==i) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num>0) countP++; } } else if(selection==2) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num<0) countN++; } } else if(choice==iii) { for(int i=0; i<tot; i++) { num = browse.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==v) { for(int i=0; i<tot; i++) { num = scan.nextInt(); if(num>0) countP++; else if(num==0) countZ++; } } else if(option==6) { for(int i=0; i<tot; i++) { num = browse.nextInt(); if(num<0) countN++; else if(num==0) countZ++; } } else if(option==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("\northwardAt that place is only one Positive Number Found."); else Arrangement.out.println("\nIn that location are " +countP+ " Positive Numbers Plant."); } if(countN!=0) { if(countN==i) System.out.println("\nAt that place is simply 1 Negative Number Found."); else System.out.println("\nThere are " +countN+ " Negative Numbers Plant."); } if(countZ!=0) { if(countZ==1) Organisation.out.println("\nThere is only i Zilch establish."); else Organisation.out.println("\nThere are " +countZ+ " Zero Found."); } } else System.out.println("\nInvalid Input!"); } }
Here is its sample run with user input half-dozen as choice to count negative number and zero simply, 6 as size to count based on 6 numbers, 1, ii, -iv, -45, 0, 23 as six numbers:
Same Program in Other Languages
- C Count Positive Negative Zero
- C++ Count Positive Negative Zero
Java Online Test
« Previous Program Next Plan »
Source: https://codescracker.com/java/program/java-program-count-positive-negative-zero.htm
0 Response to "Java Program That Reads a Series of Positive Numbers and Counts the Number of Inputs"
Postar um comentário