GeekInterview.com
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

Showing Answers 1 - 2 of 2 Answers
vasi7999

Answered On : Jun 3rd, 2012

View all answers by vasi7999

The c program is as follows :

Code
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. void main()
  5. {
  6.  
  7. int a[10],n=0,o=0,z=0,l;
  8. clrscr();
  9.  
  10. Enter the number of digits in the given number : ");
  11. scanf("%d",&l);
  12.  
  13. Enter the Binary number one by one: ");
  14.  
  15. for(i=0;i<l;i++)
  16. {
  17. scanf("%d",&a[i]);
  18. }
  19.  
  20. for(i=0 ; i<l ;   i++)
  21. {
  22. n=a[i];
  23.  
  24. if(n==0)
  25. o++;
  26.  
  27. else
  28. z++;
  29.  
  30. }
  31.  
  32. The Number of Zeros is : %d",z);
  33.  
  34. The Number of Ones is : %d",o);
  35.  
  36. getch();
  37.  
  38. }

  
Login to rate this answer.
Arupananda Pradhan

Answered On : Aug 13th, 2012

Code
  1. #include<stdio.h>
  2. int main()
  3.  {
  4.     int no,i,count =0;
  5.     printf("enter a number");
  6.     scanf("%d",&no);
  7.     for(i =0,i<16;i++)
  8.      {
  9.         if((no & 0x8000) == 0x8000)
  10.            count++;
  11.         no = no << 1;
  12.     }  // end of loop
  13.  
  14.  printf("The number of 1 in %d is %d",no,count);
  15.  
  16. return 0;
  17. } //main function end<pre>

    Code
    1. #include<stdio.h></span></div></li>
    2. <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>
    3. <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>
    4. <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>
    5. <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>
    6. <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>
    7. <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>
    8. <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>
    9. <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>
    10. <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>
    11. <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>
    12. <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>
    13. <li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"></div></li>
    14. <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>
    15. <li style="background: #fcfcfc"><div style="font-family: 'Courier New', Courier, monospace font-weight: normal font-style: normal"></div></li>
    16. <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>
    17. <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>

Yes  1 User has rated as useful.
  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

C Interview Questions

C++ Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.