Advertisement
tpaper

Untitled

May 7th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.31 KB | None | 0 0
  1. #define MAX 1000
  2. #include <stdio.h>
  3.  
  4. int conta(int n){   //restituisce il numero di cifre 1 presenti in n rappresentato in base 2
  5.     int t = 0;
  6.     while (n>0) {
  7.         if(n%2) t++;
  8.         n = n/2;
  9.     }
  10.     return t;
  11. }
  12.  
  13. int main() {
  14.     int i,t = 0;
  15.     for(i=0;i<=MAX;i++) t+= conta(i);
  16.     printf("Tot: %d\n",t);
  17.     return 0;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement