#include<stdio.h>
void nhap(float &x, int &n)//Nhap hai so x va n
{
printf("\nNhap vao x = ");
scanf("%f", &x);
do {
printf("\nNhap vao n = ");
scanf("%d", &n);
if (n<0)
{
printf("\nSo ban vua nhap khong hop le!");
printf("\nXIN VUI LONG NHAP LAI!");
}
} while (n<0);
}
double Power(float &x, int &n)//Ham luy thua
{
double kq=1;
int i;
for (i=1;i<=n;i++)
kq*=x;
return kq;
}
long giaiThua(int p)//Ham giai thua
{
double kq=1;
int i;
for (i=1;i<=p;i++)
kq*=i;
return kq;
}
int main()
{
double s1=0,s2=1,kq;
int n,i=1,n2=2;
float x,tempx=0,temps=1.0;
nhap(x,n);
kq= Power(x,n);
printf("\nKet qua x^n = %.2f",kq); // Tinh va xuat ket qua x^n
printf("\n***********************************");
while (i<=n)
{
tempx=x+i;
s1=s1+(Power(tempx,i))/(Power(temps,n2));//Tinh S1
temps++;
i++;
}
printf("\nKet qua cua S1 = %.2f", s1);//Xuat kq S1
printf("\n***********************************");
for (i=1;i<=n;i++)
s2=s2+(Power(x,i)/(giaiThua(i)));//Tinh S2
printf("\nKet qua cua S2 = %.2f", s2); //Xuat kq S2
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.