Tính tổng S(x, n) = x^2 + x^4 + x^6 +…+ x^2n

Yêu cầu:
– Nhập vào số nguyên dương n
– Tính tổng S(x, n) = x^2 + x^4 + x^6 +…+ x^2n

Giải thuật:
– Dùng vòng lặp for, do while để duyệt biến i từ 1 tới n
– Tính tổng: s = s + x^2i
– Dùng hàm pow để tính x^2i

Code

/************************************************************
* Author: VNCODING
* History
* 2016/12/14        first create    VNCODING
*************************************************************/

#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
	printf("S(x, n) = x^2 + x^4 + x^6 +…+ x^2n\n");
	int i, n;
    float x;
    float s = 0.0;
    printf("x = ");
    scanf("%f", &x);
	do
	{
		printf("n = ");
		scanf("%d", &n);
	}while(n <= 0);
	
	for(i = 1; i <= n; i++)
	{
        s += pow(double(x), double(2*i));
	}
	printf("\nS(%d) = %f", n, s);
	getch();
	return 0;
}

Kết quả:

Tính tổng S(x, n) = x^2 + x^4 + x^6 +…+ x^2n
Tính tổng S(x, n) = x^2 + x^4 + x^6 +…+ x^2n

Be the first to comment

Leave a Reply