A number is generally represented in decimal system which we use in our daily life.
But a number can be represented using octal,binary and hexadecimal too.
here’s a link which has good information and you can easily understand what are these
number systems(decimal,octal,binary and hexadecimal)
Below is the code
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
main()
{
int i,j,size=0,x,k=1,value=0,temp,choice,length, sum=0;;
long int num;
char name[10],q,test[10],hex[10];
printf("MENU\n1.From binary to decimal\n2.From octal to decimal\n3.From hexa decimal to decimal\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter BINARY number\n");
scanf("%d",&num);
temp=num;
size=0;
while(temp>0)
{
temp/=10;
size++;
}
temp=num;
for(i=0;i<size;i++)
{
k=1;
x=num%10;
k=pow(2,i);
value+=k*x;
num/=10;
}
num=temp;
printf("the number in decimal form is:%d",value);
break;
case 2:
printf("enter OCTAL number\n");
scanf("%d",&num);
temp=num;
size=0;
while(temp>0)
{
temp/=10;
size++;
}
temp=num;
for(i=0;i<size;i++)
{
k=1;
x=num%10;
k=pow(8,i);
value+=k*x;
num/=10;
}
num=temp;
printf("the number in decimal form is:%d",value);
break;
case 3:
printf("enter hexa decimal number:");
scanf("%s",name);
strcpy(hex,name);
for(length=0; hex[length]!='\0'; ++length);
for(i=0; hex[i]!='\0'; ++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
sum+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
sum+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
sum+=(hex[i]-87)*pow(16,length-1);
}
printf("\n%d",sum);
}
}
#include<string.h>
#include<math.h>
#include<ctype.h>
main()
{
int i,j,size=0,x,k=1,value=0,temp,choice,length, sum=0;;
long int num;
char name[10],q,test[10],hex[10];
printf("MENU\n1.From binary to decimal\n2.From octal to decimal\n3.From hexa decimal to decimal\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter BINARY number\n");
scanf("%d",&num);
temp=num;
size=0;
while(temp>0)
{
temp/=10;
size++;
}
temp=num;
for(i=0;i<size;i++)
{
k=1;
x=num%10;
k=pow(2,i);
value+=k*x;
num/=10;
}
num=temp;
printf("the number in decimal form is:%d",value);
break;
case 2:
printf("enter OCTAL number\n");
scanf("%d",&num);
temp=num;
size=0;
while(temp>0)
{
temp/=10;
size++;
}
temp=num;
for(i=0;i<size;i++)
{
k=1;
x=num%10;
k=pow(8,i);
value+=k*x;
num/=10;
}
num=temp;
printf("the number in decimal form is:%d",value);
break;
case 3:
printf("enter hexa decimal number:");
scanf("%s",name);
strcpy(hex,name);
for(length=0; hex[length]!='\0'; ++length);
for(i=0; hex[i]!='\0'; ++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
sum+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
sum+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
sum+=(hex[i]-87)*pow(16,length-1);
}
printf("\n%d",sum);
}
}
No comments:
Post a Comment