UNIONS


This program displays x power y where x and y are floating points.Use of unions is depicted.


#include"stdio.h"
#include"math.h"
double f(double);
double e(double);
long g(double);
main()
{
double d,x,p,q,result;
int count;
union power
{
float n;
int byte;
};
union power pn;
clrscr();
printf("enter positive base,exponent:");
scanf("%lf %f",&x,&pn.n);
if(x<0)
{
printf("read instructions carefully");
getch();
exit();
}
d=x;
count=0;
while(d>1)
{
d=d/10;
count++;
}
p=d-1;
q=pn.n*(count*2.303+f(p));
result=e(q);
printf("%lf",result);
if(pn.byte==0)
printf("\npower is an integer");
else
printf("\npower is a floating point");
getch();
}
double f(double p)
{
double sum=0;
int c=1;
while(c<100)
{
sum=sum+(pow(p,c)*pow(-1,c+1))/c;
c++;
}
return sum;
}
double e(double q)
{
long double sum2=1;
double c=1;
while(c<12)
{
sum2=sum2+(pow(q,c))/g(c);
c++;
}
return sum2;
}
long g(double c)
{
long p=1;
while(c>1)
{
p=p*c;
c--;
}
return p;
}


Go to the programs page click here!!

Hosted by www.Geocities.ws

1