Sunday, October 18, 2015

C Program to count the number of zeros and ones in a particular position of binary numbers.(Input should be decimal number, convert them to binary number)

/* =====================================================================================
 * *
 * *       Filename:  posbitcount.c
 * *        Created:  Tuesday 3 February 2015
 * *       Compiler:  gcc
 * *         Author:  Manoj Kumar Patra,manojpatra.sit@gmail.com
 * *   Organization:  SCIS,University of Hyderabad
 * *
 * * =====================================================================================*/

#include<stdio.h>
int decimal_binary(int ar, int n1[]);
int main()
{
int k=0,c1,ni,pos;
int zcount=0,ocount=0;
int arr[100],j;
int n1[100];
printf("\n--------------------------------------------------------\n");
printf("How many integer you wanna insert:");
scanf("%d",&ni);
printf("Enter the elements in to the array:\n");
for(j=0; j<ni; j++)
{
scanf("%d",&arr[j]);
}
printf("\nThe elements of the array are:\n");
for(j=0; j<ni; j++)
{
printf("%d\t",arr[j]);
}
printf("\n--------------------------------------------------------\n");
printf("Enter the position of the bit:");
scanf("%d",&pos);
for(j=0; j<ni; j++)
{
c1=decimal_binary(arr[j],n1);
printf("\nThe binary value of %d is:",arr[j]);
for(k=0; k<c1; k++)
{
printf("%d",n1[k]);
}
if(n1[pos] == 0)
{
zcount++;
}
else
{
ocount++;
}
}
printf("\n--------------------------------------------------------\n");
printf("Position count starts from zero(0)\n");
printf("No of zero in %d position=%d\n",pos,zcount);
printf("No of one in %d position=%d",pos,ocount);
printf("\n--------------------------------------------------------\n");
printf("\n");
return 0;
}
int decimal_binary(int dn, int ar[])
{
int k=0,quo = dn;
int i=1,j;
int binaryNumber[100];
while(quo!=0)
{
binaryNumber[i++]= quo % 2;
quo = quo / 2;
}
for(j = i -1 ;j> 0;j--)
{
ar[k]=binaryNumber[j];
k++;
}
return k;
}

OutPut:

No comments:

Post a Comment