fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int count=0;
  5. int BinDigit(int n){
  6. count++;
  7. if(n==1){
  8. return 1;
  9. }
  10. else{
  11. return (BinDigit(n/2)+1);
  12. }
  13. }
  14. void tester(){
  15. int n,ans;
  16. printf("Enter a number to find number of digits required to represent in Binary form\n");
  17. scanf("%d",&n);
  18. ans=BinDigit(n);
  19. printf("Number of digits required to represent %d is %d\n",n,ans);
  20. }
  21. void plotter(){
  22. srand(time(NULL));
  23. int n,r;
  24. FILE *f1;
  25. f1=fopen("BinDigit.txt","w");
  26. n=2;
  27. while(n<=1500){
  28. count=0;
  29. r=BinDigit(n);
  30. fprintf(f1,"%d\t%d\n",n,count);
  31. n=n*2;
  32. }
  33. fclose(f1);
  34. }
  35. void main(){
  36. int ch;
  37. printf("Enter your choice:\n1.Tester\n2.Plotter\n");
  38. scanf("%d",&ch);
  39. switch(ch){
  40. case 1:
  41. tester();
  42. break;
  43. case 2:
  44. plotter();
  45. break;
  46. default:
  47. printf("Invalid choice!!\n");
  48. }
  49. }
Success #stdin #stdout 0s 5316KB
stdin
1
stdout
Enter your choice:
1.Tester
2.Plotter
Enter a number to find number of digits required to represent in Binary form
Number of digits required to represent  5225 is 13