Thursday, September 29, 2011

WAP to convert 10 base number into base 2, base 8, base 16

#include<stdio.h>
#include<conio.h>
con(int,int);
void main()
{
int num,ch;
clrscr();
printf("enter a  decimal number\n");
scanf("%d",&num);
printf("1-binary\n");
printf("2-hexadecimal\n");
printf("3-ocatal\n");
printf("\n");
printf("enter your choice\n");
scanf("%d",&ch);

switch(ch)
{
case 1:
    printf("binary of given decimal number is\n");
    con(num,2);
    break;
case 2:
    printf("hexadecimal of given decimal number is\n");
    con(num,16);
    break;
case 3:
    printf("octal of given decimal number is\n");
    con(num,8);
    break;
default:
    printf("wrong choice\n");
    break;

}
printf("\n");
getch();
}
con(int num,int c)
 {
    int i=0,j,rem;
    char a[20];
    while(num>0)
    {
        rem=num%c;
        num=num/c;
        if(rem>9 && rem<16)
            {
            a[i]=rem-10+'A';
            i++;
            }
        else
        {
        a[i]=rem+'0';
        i++;
        }
    }
    for(j=i-1;j>=0;j--)
    printf("%c",a[j]);

    }

output:

enter your choice
1
binary of given decimal number is
100010
enter a  decimal number
45                                                                             
1-binary                                                                       
2-hexadecimal                                                                  
3-ocatal                                                                       
                                                                               
enter your choice                                                              
2                                                                              
hexadecimal of given decimal number is                                         
2D                                                                             
enter a  decimal number                                                        
78                                                                             
1-binary                                                                       
2-hexadecimal                                                                  
3-ocatal                                                                       
                                                                               
enter your choice                                                              
3                                                                              
octal of given decimal number is                                               
116                                                                            
                                                                               
#include<conio.h>
#include<stdio.h>

No comments:

Post a Comment