Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*this program converts number in any base to any other base (limit>> only upto hex number, ie, base 16)*/
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- int entry();// gets any base input and converts it to decimal
- int main(void)
- {
- int ip_num,ip_base,op_num,op_base,i=0,cont_flag,acc[20];
- char base_digits[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- do
- {
- ip_num=entry();
- printf("choose the base to convert to:");
- scanf("%d",& op_base);
- puts("\nin main()\n\nexecuting for() loop . . .\n\n");
- for(i=0;i<20 && ip_num!=0;i++) // converts to i/p to required o/p base
- {
- acc[i]=(ip_num % op_base);
- ip_num=ip_num/op_base;
- printf("index: %d \n",i);
- if(ip_num==0)
- break;
- if(i==19) {
- puts("array acc[] full");
- break;
- }
- }//end of for()
- printf("\nconverted number in base %d is:",op_base);
- for(i;i>=0;i--)// prints the number to screen
- {
- printf("%c",base_digits[acc[i]]);
- }
- printf("\n\nconvert another number? press\n 1(yes)\n 0(no)\n:");
- scanf("%d",& cont_flag);
- }while(cont_flag==1);// cont_flag lets user decide whether they want to convert another number or not
- }
- int entry()
- {
- char sh[100],*stop;
- int ip_num,ip_base;
- printf("Enter input number: ");
- scanf("%s",&sh);
- printf("enter input base: ");
- scanf("%d",&ip_base);
- ip_num = strtol(sh,&stop,ip_base);
- return(ip_num);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement