I have a command-line converter i'm working on. The basics are all finished, just looking for ways to improve it.

Code:
#include <stdio.h>
int main()
{
   int n, c, k;
   printf("Enter a decimal number:\n");
   scanf("%d", &n);
   printf("%d in binary is:\n", n);
   for(c=31; c>=0; c--)
   {
      k=n>>c;
      if (k&1)
         printf("1");
      else
         printf("0");
   }
   printf("\n");
   getchar();
   getchar();
   return 0;
}

One thing I'm particularly looking for is a way to remove those leading zeros. If you compile it yourself you will see what I mean. it does that because it treats your input as a 32-bit number, even if it isn't, so it always has 32 bits in it.[/code]

Code:
#include <stdio.h>
int main()
{
   int n, c, k,f;
   f=0;
   printf("Enter a decimal number:\n");
   scanf("%d", &n);
   printf("%d in binary is:\n", n);
   for(c=31; c>=0; c--)
   {
      k=n>>c;
      if (k&1){
         printf("1");
         f = 1;
      } else if(f) {
         printf("0");
      }
   }
   printf("\n");
   getchar();
   getchar();
   return 0;
}
Awesome! Thanks a bunch.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement