Java Program to Find Number of 1’s in an Integer Array
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
