Monday, 5 September 2016

Number of Binary Digits Occurence in a given Binary Number

import java.util.*;

class BinaryOccurence{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Howmany Elements you want to test? ");
int T=sc.nextInt();
int arr[] = new int[T];
int k,N,c1,c0,i=0,j=0;
int num;
System.out.println("Enter the Binary numbers ");
for(k=0;k<T;k++){
          arr[k]=sc.nextInt();
num=arr[k];
}

for(k=0;k<T;k++){
      
num=arr[k];

 while(num!=0){
c1=num%10;    
c0=num%10;
num=num/10;
if(c1==1)
{
i++;
}
if(c0==0)
{
j++;
}
  }
System.out.println("Number of 0's in the given BinaryNumber = "+j );
System.out.println("Number of 1's  in given BinaryNumber= "+i);

i=0;
j=0;
}

}

}


Post a Comment