Series: Subject: Topic:
Question: 4 of 196

# C language

Write a function to find how many binary 1s nd 0s present in a number
Asked by: yekkalarajesh | Member Since May-2012 | Asked on: May 25th, 2012

View all questions by yekkalarajesh

vasi7999

Answered On : Jun 3rd, 2012

The c program is as follows :

```Code#include<stdio.h>
#include<conio.h>

void main()
{

int a[10],n=0,o=0,z=0,l;
clrscr();

printf("
Enter the number of digits in the given number : ");
scanf("%d",&l);

printf("
Enter the Binary number one by one: ");

for(i=0;i<l;i++)
{
scanf("%d",&a[i]);
}

for(i=0 ; i<l ;   i++)
{
n=a[i];

if(n==0)
o++;

else
z++;

}

printf("
The Number of Zeros is : %d",z);

printf("
The Number of Ones is : %d",o);

getch();

}```

Answered On : Aug 13th, 2012

```Code#include<stdio.h>
int main()
{
int no,i,count =0;
printf("enter a number");
scanf("%d",&no);
for(i =0,i<16;i++)
{
if((no & 0x8000) == 0x8000)
count++;
no = no << 1;
}  // end of loop

printf("The number of 1 in %d is %d",no,count);

return 0;
} //main function end<pre>Code#include<stdio.h></span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"><span style="color: #993333">int</span> main<span style="color: #009900">(</span><span style="color: #009900">)</span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"><span style="color: #009900">{</span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">  <span style="color: #993333">int</span> no<span style="color: #339933">,</span>i<span style="color: #339933">,</span>count <span style="color: #339933">=</span><span style="color: #0000dd">0</span><span style="color: #339933"></span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">  <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066">printf</span></a><span style="color: #009900">(</span><span style="color: #ff0000">"enter a number"</span><span style="color: #009900">)</span><span style="color: #339933"></span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">  scanf<span style="color: #009900">(</span><span style="color: #ff0000">"%d"</span><span style="color: #339933">,&</span>no<span style="color: #009900">)</span><span style="color: #339933"></span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">  <span style="color: #b1b100">for</span><span style="color: #009900">(</span>i <span style="color: #339933">=</span><span style="color: #0000dd">0</span><span style="color: #339933">,</span>i<span style="color: #339933"><</span><span style="color: #0000dd">16</span><span style="color: #339933"></span>i<span style="color: #339933">++</span><span style="color: #009900">)</span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">  <span style="color: #009900">{</span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">    <span style="color: #b1b100">if</span><span style="color: #009900">(</span><span style="color: #009900">(</span>no <span style="color: #339933">&</span> 0x8000<span style="color: #009900">)</span> <span style="color: #339933">==</span> 0x8000<span style="color: #009900">)</span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">     count<span style="color: #339933">++</span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">    no <span style="color: #339933">=</span> no <span style="color: #339933"><<</span> <span style="color: #0000dd">1</span><span style="color: #339933"></span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal">  <span style="color: #009900">}</span> <span style="font-style: italic">// end of loop</span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"><a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066">printf</span></a><span style="color: #009900">(</span><span style="color: #ff0000">"The number of 1 in %d is %d"</span><span style="color: #339933">,</span>no<span style="color: #339933">,</span>count<span style="color: #009900">)</span><span style="color: #339933"></span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"><span style="color: #b1b100">return</span> <span style="color: #0000dd">0</span><span style="color: #339933"></span></div></li>
<li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"><span style="color: #009900">}</span> <span style="font-style: italic">//main function end</span></div></li></ol></div></p></pre>```

1 User has rated as useful.

### Question Categories

C Interview Questions

C++ Interview Questions