Advertisement
horselurrver

To base

Aug 2nd, 2016
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. void to_base(unsigned long n, int base);
  3.  
  4. int main(void){
  5.     unsigned long number;
  6.     int base=2;
  7.     printf("Enter an integer (q to quit):\n");
  8.     while(scanf("%lu", &number)==1 && (base>=2 && base<=10)){
  9.         printf("Enter a base to convert to (2-10):");
  10.         scanf("%d", &base);
  11.         printf("Binary equivalent: ");
  12.         to_base(number, base);
  13.         putchar('\n');
  14.         printf("Enter an integer (q to quit):\n");
  15.     }
  16.    
  17.     printf("Done.\n");
  18.    
  19.     return 0;
  20. }
  21.  
  22. void to_base(unsigned long n, int base){
  23.     int r;
  24.    
  25.     r=n%base;
  26.     if (n>=base)
  27.         to_base(n/base, base);
  28.     putchar('0'+r);
  29.    
  30.     return;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement