Base Conversion- Number (Decimal) to Any Base -C Program

Program:
//  #include<conio.h>
 #include<stdio.h>
  void main()
  {
      int b,n,i,r,digit,p,count=0;
      char a[100];// clrscr();
      printf("\nEnter the decimal number:\n");
     scanf("%d",&n);
      printf("\nEnter the base to be converted:\n");
    scanf("%d",&b);
      p=n;
      do
    {
        r=p%b;
        digit='0'+r;
        if(digit>'9')
            digit=digit+7;
        a[count]=digit;
        count++;
        p=p/b;
         } while(p!=0);
      printf("\nbase %d equivalent of num %d is ",b,n);
      for(i=count-1;i>=0;--i)
        printf("%c",a[i]);
    printf(".\n");
//  getch();
  }

Output:
nn@linuxmint ~ $ gcc c16.c
nn@linuxmint ~ $ ./a.out

Enter the decimal number:
10

Enter the base to be converted:
2

base 2 equivalent of num 10 is 1010.
nn@linuxmint ~ $ ./a.out

Enter the decimal number:
10

Enter the base to be converted:
8

base 8 equivalent of num 10 is 12.
nn@linuxmint ~ $ ./a.out

Enter the decimal number:
15

Enter the base to be converted:
16

base 16 equivalent of num 15 is F.
nn@linuxmint ~ $

3 comments:

Related Posts Plugin for WordPress, Blogger...