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

13 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. 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
  5. This post is good enough to make somebody understand this amazing thing, and I’m sure everyone will appreciate this interesting things. Hex To Decimal

    ReplyDelete
  6. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    hadoop training in chennai

    hadoop training in tambaram

    salesforce training in chennai

    salesforce training in tambaram

    c and c plus plus course in chennai

    c and c plus plus course in tambaram

    machine learning training in chennai

    machine learning training in tambaram

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...