LegoDrifter

Binary array

Oct 29th, 2020
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int main()
  6. {
  7. int n,exp=1,rez=0;
  8. printf("Vnesi n za nizata\n");
  9. scanf("%d",&n);
  10. int niza[n];
  11. for(int i=0;i<n;i++)
  12. {
  13. scanf("%d",&niza[i]);
  14. }
  15. for(int i=0;i<n;i++)
  16. {
  17. printf("%d ",niza[i]);
  18. }
  19. for(int i=n-1;i>=0;i--)
  20. {
  21. if(niza[i]==1)
  22. {
  23. rez += exp;
  24.  
  25. }
  26. exp *=2;
  27. }
  28. printf("Rez e! %d\n",rez);
  29.  
  30. return 0;
  31.  
  32. }
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment