In the previous article, we have seen Java Program to Form Two Numbers (of 2 digit) with Minimum Sum Using Array Elements
In this article we will see how to find number of 1’s in an integer array using Java Programming language.
Java Program to Find Number of 1’s in an Integer Array
Prerequisite:
See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.
- How to Declare an Array in Java?
- How to Instantiate an Array in Java?
- How to Initialize an Array in Java?
Let’s see different ways to find number of 1’s in an integer array.
Method-1: Java Program to Find Number of 1’s in an Integer Array By Static Initialization of Array Elements
Approach:
- Create an array with elements which is the original array i.e
arr[]
. - Declare an integer variable say ‘
count
‘ and assign value as 0, which will store the number of 1’s present in the array. - Take a for loop iterate the original array.
- Check if anywhere element 1 is found then increment the value of
count
. - Finally, print the value of
count
.
Program:
import java.util.*; public class Main { public static void main(String args[]) { //Array declared with array elements int arr[] ={1,2,3,1,4,5,1,6}; System.out.print("Original Array: "); //printing the original array for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); //declaring int varibale count and assigning value 0 int count = 0; // Traversinng the array looking for the element 1 for(int i = 0; i<arr.length; i++) { if(arr[i]==1) { count++; } } System.out.println("There are "+count+" numbers of 1's present in the array"); } }
Output: Original Array: 1 2 3 1 4 5 1 6 There are 3 numbers of 1's present in the array
Method-2: Java Program to Find Number of 1’s in an Integer Array By Dynamic Initialization of Array Elements
Approach:
- Create scanner class object.
- Ask use length of the original array i.e
arr[]
. - Initialize the array with given size.
- Ask the user for input of array elements to the original array.
- Declare an integer variable say ‘
count
‘ and assign value as 0, which will store the number of 1’s present in the array. - Take a for loop iterate the original array.
- Check if anywhere element 1 is found then increment the value of
count
. - Finally, print the value of
count
.
Program:
import java.util.*; public class Main { public static void main(String args[]) { //Scanner class object created Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); int arr[] = new int[num]; System.out.print("Enter the elements: "); //taking input of array elements for (int i = 0; i < num; i++) { arr[i] = sc.nextInt(); } System.out.print("Original Array: "); //printing the original array for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); //declaring int varibale count and assigning value 0 int count = 0; // Traversinng the array looking for the element 1 for(int i = 0; i<arr.length; i++) { if(arr[i]==1) { count++; } } System.out.println("There are "+count+" numbers of 1's present in the array"); } }
Output: Enter the number of elements in the array: 10 Enter the elements: 1 2 3 1 5 1 7 8 9 1 Original Array: 1 2 3 1 5 1 7 8 9 1 There are 4 numbers of 1's present in the array
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Related Java Articles: