C++ program to display a digits position according to available 1’s in its equivalent binary Number. ie if a user input any non negative integer number from the keyboard, We have to show the position of that number.

Example

Suppose user entered 17 from the keyboard then the total number of 1 digits available in its binary equivalent is : 2 . Now we have 5 number below that 17 that have 2 one’s in their binary equivalent. So the position of 17 is 6.

The source code of this program is :

/* Program to count total no of 1's available in any decimal number and find out its position with same counting 1's number
made by : rakesh kumar
*/
#include
#include
using namespace std;
int binary_count(int n)
{
int count=0;
int rem;
while(n!=0)
{
rem = n%2;
if ( rem ==1)
count++;
n = n/2;
}
return(count);
}

int main()

{

int n,one,res,count=0;;

cout<<“\n Enter any number N :”; cin>>n;

one = binary_count(n);

for(int i=1;i<n;i++)

{

res = binary_count(i);

if(res==one)

count++;

}

cout<<“\n “< return 0;

}

Please remember this that the program was developed in Windows 10 using Dev C++ IDE. The output of the above program is as follows.

*You may also like these*

cbsetoday.com is my online home, here I share all the information related to education, programming - and everything else that attract me. You can get in touch with me in these social sharing websites