Answer:
// Program is written in C Programming Language
// Comments are used for explanatory purpose
// Program starts here
#include<stdio.h>
#include<math.h>
//Function to Convert to float
void To float(int num, int I)
{
//Create a kount variable
int kount;
// Start an iteration
for(kount=i-1; kount>0; kount--)
{
if((num>>kount) && 1) {printf("1");}
else { printf("0"); }
}
}
// Create a user defined variable
typedef union {// Definition
float Number;
struct
{
// Mantissa
unsigned int mant : 23;
// Exponent
unsigned int exp : 8;
// Sign
unsigned int sign: 1;
} raw;
} myfloat; // Variable name
// Create print segment
void printsegment(myfloat var)
{
printf("%d |", var.raw.sign);// Sign
To float(var.raw.exp,8); // Exponent
printf("|");
To float(var.raw.mant,8); // Mantissa
printf("\n");
}
// Function to Convert to Real
unsigned int ToReal(int* dig[], int l, int h)
{
unsigned int f = 0, I;
Start an iteration
for(I = h; I>=l;I--)
{
// Calculate individual value
f = f + dig[I] * pow(2,h-1);
}
return f;
}
// Main method start here
int main()
{
printf("Floating Point Conversion\n");
printf("Select any of the following options\n");
printf("1. Decimal to IEEE754 Conversion\n");
printf("2. IEEE754 to Decimal Conversion\n");
printf("3. Quit");
// Declare integer variable for option
int opt;
// Prompt to select option
printf("Select an option; Option 1 to 3: ");
scanf("%d", $opt);
if(opt == 1)
{
printf("You have selected option 1");
// Declare a user defined variable and a system defined variable
myfloat var; float number;
// Accept input
scanf("%d", number);
// Check for special cases
if(isnan(number/0.0))// Not a number
{
printf("Not a Number");
}
else
{
var.f = number;
// Print Sign
printf("%d | ", var.raw.sign);
// Print Exponent
ToFloat(var.raw.exp,8);
printf(" | ");
// Print Mantissa
ToFloat(var.raw.mant,23);
}
}// End of option 1;
// Beginning of option 2
else if(opt == 2)
{
printf("You have selected option 2");
// Declare an array and two integer variables
unsigned int number[32];
int ctrlno, I = 0;
// Accept input by through an iteration
for(int k = 0; k < 32; k++)
{
// Create a label
label: scanf("%d", ctrlno);
// Check for special cases
if(isnan(ctrlno/0.0))// Not a number
{
printf("Not a Number"); I++;
break;
}
else if(ctrlno>1 || ctrlno < 0)
{
printf("Invalid Number\n Please enter a valid digit");
goto label;
}
else {
// Assign number to array
number[k] = ctrlno;
}
// Check validity of number
if(I != 0)
{
printf(" Invalid Number Representation");
}
else
{
// Declare user defined variable
myfloat var;
// Get sign
var.raw.sign = number[0];
// Get mantissa; From to 31
unsigned f = ToReal(number,9,31);
var.raw.mant = f;
// Get exponent; 1 to 8
f = ToReal(number,1,8);
var.raw.exp = f;
// Print Output
printf("The converted digit is ");
printf("%f", var.f);
}
else
{
// Quit Application
break;
}
return 0;
}