Counting numbers of ones in binary representation of an interger

Problem Statement
Write a C program that will read a
non-negative integer and will count the number
of 1's in the binary representation of it.


#include<stdio.h> int main() { int n,count; printf("enter n \n"); scanf("%d",&n); count=0; while(n>=1) { if(n%2!=0) { count=count+1; n=n/2; } else { n=n/2; } } printf("the number of ones in the binary rep. of number is %d \n",count); return 0; }

No comments:

Post a Comment