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 ~ $

8 comments:

  1. genus!!! Thanks a lot.

    ReplyDelete
  2. why u have done
    if(digit>'9')
    digit=digit+7;

    ReplyDelete
  3. @Nishant Kumar

    Because after 9 you have 7 ASCII character in between 9 ans A and you have to use A after 9 instead to 10(not in case of base 10/decimal).

    ReplyDelete
  4. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Best Devops Training in pune
    Microsoft azure training in Bangalore
    Power bi training in Chennai

    ReplyDelete

  5. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  6. I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
    python interview questions and answers
    python tutorials
    python course institute in electronic city

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...