#include <iostream.h>
#include <conio.h>
union sample
{
int r1 ;
float r2 ;
} ;
struct triplet
{
int a ;
int b ;
int flag ; /* 'flag' is used to indicate the type */
union sample c ; /* of value stored in union 'c' */
} ;
void main ( )
{
void find_divison ( struct triplet X[ ] ) ;
struct triplet X[5] ;
int i ;
clrscr () ;
cout<<"Input numbers :\n";
for ( i = 0; i < 5; i++ )
{
cin>>X[i].a;
cin>>X[i].b;
}
find_divison ( X ) ;
/* printing the result in correct format 'int' or 'float' */
for ( i = 0; i < 5; i++ )
{
cout<<"\nThe result of divison of "<<X[i].a<<"&"<<X[i].b<<" : ";
if ( X[i].flag == 0 )
cout<<X[i].c.r1;
else
cout<<X[i].c.r2;
}
getch ( ) ;
}
void find_divison ( struct triplet X[] )
{
int i ;
for ( i = 0; i < 5; i++ )
{
int temp ;
if ( X[i].a < X[i].b ) /* Swap if 'a' is less than 'b' */
{
temp = X[i].a ;
X[i].a = X[i].b ;
X[i].b = temp ;
}
if ( ( X[i].a % X[i].b ) != 0 )
{
/* if the result of divison is 'float' */
X[i].flag = 1 ; /* flag = 1, indicates 'float' result */
X[i].c.r2 = (float) X[i].a / (float) X[i].b ;
}
else
{
/* if the result of divison is 'int' */
X[i].flag = 0 ; /* flag = 0, indicates 'int' result */
X[i].c.r1 = X[i].a / X[i].b ;
}
}
}
0 comments:
Post a Comment