by Dinesh Thakur

Algorithm for Ones Present Inputted Array:

step 1: set arr_size,j, count as three static variables of integer type

step 2: read arr_size

step 3: create an integer array arr[] of size arr_size

step 4: initialize i=0 

 

step 5: repeat through step-7 while i less than arr_size

step 6: readarr[i]

step 7: i=i+ 1

step 8: initialize i=0

step 9: repeat through step-15 while i less than arr_size

step 10: j=arr[i]

step 11: repeat through step-14 while j greater than 0

step 12: k=j%10

step 13: if k equals to one then count=count+ 1

step 14:j=j/10

step 15: i=i+ 1

step 16: print count

step 17: Exit

Here is the Java Example for Ones Present Inputted Array:

import java.util.Scanner;
public class OnesPresentInputtedArray
{
              public static int size;
              public static int j;
              public static int count;
           public static void main(String args[])
      {
              Scanner s=new Scanner(System.in);
              System.out.print("Enter the Number of Elements :");
              size=s.nextInt();
              int arr[]=new int[size];
              System.out.println("Enter the Elements : ");
              for(int i=0;i<size;i++)
                 {
                     System.out.println("Enter the "+i+"th Index Value");
                     arr[i]=s.nextInt();
                  }
                     for(int i=0;i<size;i++)
                        {
                           j=arr[i];
                           while(j>0)
                              {
                                 int k=j%10;
                                   if(k==1)
                                     {
                                         count++;
                                      }
                                          j=j/10;
                               }
                         }
System.out.print("No. of 1's Present in the Inputed Array is:-> ");
System.out.println(count);
      }
}

how many 1's are present in an inputted array