Slip no : 30_2 Write a C
prog to convert decimal no into binary,octal, hexadecimal write seperate user
defined function for each option
#include<stdio.h>
#include<conio.h>
void
binary(int no)
{
int
a[20],i; i=0; while(no>0)
{
a[i]=no%2;
no=no/2;
i++;
}
printf("\n
Binary no is : "); for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
}
void
octal(int no)
{
int
a[20],i; i=0; while(no>0)
{
a[i]=no%8;
no=no/8;
i++;
}
printf("\n
Octal no is : "); for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
}
void
hexa(int no)
{
int
a[20],i; i=0; while(no>0)
{
a[i]=no%16;
no=no/16;
i++;
}
printf("\n
Hexadecimal no is : "); for(i=i-1;i>=0;i--)
{ if(a[i]<10) printf("%d",a[i]); else
{
switch(a[i])
{
case 10: printf("A"); break;
case 11: printf("B"); break;
case 12:printf("C"); break;
case 13:printf("D"); break;
case 14:printf("E");
break;
case 15:printf("F"); break;
}
}
}//end of for
}
main()
{
int n;
printf("\n
Enter no : "); scanf("%d",&n); binary(n);
octal(n);
hexa(n);
getch();
}
No comments:
Post a Comment