[ create a new paste ] login | about

Link: http://codepad.org/X4uCIc79    [ raw code | output | fork | 8 comments ]

C, pasted on Aug 17:
#include<stdio.h>
#include<conio.h>

int main()
{
	int i, n;
	float S;
	S = 0;
	i = 0;
	do
	{
		printf("\nNhap n: ");
		scanf("%d", &n);
		if(n < 1)
		{
			printf("\nN phai lon hon hoac bang 1. Xin nhap lai !");
		}
	}while(n < 1);

	while(i < n)
	{
		S = S + (float)(2 * i + 1) / (2 * i + 2);
		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 hnit03 on Jul 25
tại sao phải dùng float trước (2*i+1)/(2*i+2). không dùng có được không ạ
reply
posted by czczc on Feb 1
vd (2*1+1)/(2*1+2) sẽ ra số phức => nên t phải dùng float
reply
posted by MinhNhat on Feb 1
#include <iostream>
using namespace std;

int main ()
{
int n;
cin >> n;
double s = 0;
for (int i = 0; i <= n; i++)
{
s = (double) (2*i+1)/(2*i+2) + s;
}
cout << s;
return 0;
/*HuynhMinhNhat*/
}

reply
posted by MinhNhat on Feb 1
#include <iostream>
using namespace std;

int main ()
{
int n;
cin >> n;
double s = 0;
for (int i = 0; i<n; i++)
{
s = (double) (2*i+1)/(2*i+2) + s;
}
cout << s;
return 0;
/*HuynhMinhNhat*/
}
reply
posted by incarnation16 on May 21
#include <stdio.h>

int main()
{
int i, n;
float S;
S = 0;
do
{
printf("\nNhap n: ");
scanf("%d", &n);
if (n < 1)
{
printf("\nN phai lon hon hoac bang 1. Xin nhap lai !");
}
} while (n < 1);

for (i = 0; i < n; i++)
{

S = S + (float)(2*i + 1)/(2*i + 2);
}
printf("\nTong la: %f", S);
}
reply
posted by Minhhy89pyuo on Jan 4
Tại sao i<n chứ ko phải là i<=n
reply
posted by Minhhy89pyuo on Jan 4
Dùng kiểu double thay thế float đc ko ạ
reply
posted by 20223768 on Sep 9
#include <iostream>
using namespace std;
int main()
{
int n; cin >>n;
double s=0;
for (double i=0; i<=n; i++)
{
s=s+((2*i+1)/(2*i+2));
}
cout << s;
return 0;
}
reply