[ create a new paste ] login | about

Link: http://codepad.org/VhaHrabt    [ raw code | output | fork | 2 comments ]

C, pasted on Aug 31:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
	int i, n;
	float x, S, T;
	long M;
	printf("\nNhap x: ");
	scanf("%f", &x);
	do
	{
	printf("\nNhap n: ");
	scanf("%d", &n);
	  if(n < 1)
	  {
		  printf("\n N phai >= 1. Xin nhap lai !");
	  }

	}while(n < 1);

	S = 0;
	T = 1;
	M = 0;
	i = 1;

	while(i <= n)
	{
		T = T * x;
		M = M + i;
		S = S + pow(-1, (float)i) * T/M;
		i++;
	}
	printf("\nTong la %f", S);
	getch();
	return 0;
}


Output:
1
Line 17: error: conio.h: No such file or directory


Create a new paste based on this one


Comments:
posted by yonnon on Jul 12
#include<bits/stdc++.h>

using namespace std;
int main(){
int n;
int x;
cout <<"Enter x= ";
cin >> x;
cout <<"Enter n= ";
cin >> n;
int temp=0;
float S=0;
for(int i=1; i<=n; ++i){
temp+=i;
S+= pow(-1.0,i) * (pow(static_cast<float>(x),i)/temp);
}
cout << "Sum = "<<S<<
endl;
cin.ignore();
return 0;
}
reply
posted by cuong.luucb8921 on Aug 28
#include<stdio.h>

int main(){
int n;
float x;

do
{
printf("Nhap vap n:\n");
scanf("%d",&n);
if(n < 1)
{
printf("n phai lon hon bang 1\n");
}
}while(n < 1);

do
{
printf("Nhap vap x:\n");
scanf("%f",&x);
if(x == 0)
{
printf("x phai khac o\n");
}
}while(x == 0);

int i;
int s=0;
float sum=0;

for(i=1;i<=n;i++)
{
s = s + i;
sum = sum + (pow(-1,i)*pow(x,i))/s;
}

printf("sum = %f",sum);
return 0;
}
reply