[ create a new paste ] login | about

Link: http://codepad.org/bVsqTj3m    [ raw code | output | fork ]

C, pasted on Apr 21:
#include<stdio.h>
#include<conio.h>

float deQuy(int n)
{
	if(n == 1)
		return 1;
	float tong = 1;
	for(int i = 2; i <= n; i++)
	{
		tong += i;
	}
	return deQuy(n - 1) + 1/tong;
}

float deQuyDuoi(int n, float y = 1)
{
	if(n == 1)
		return y;
	float tong = 1;
	for(int i = 2; i <= n; i++)
	{
		tong += i;
	}
	return deQuyDuoi(n - 1, y + 1.0/tong);
}

float khuDeQuy(int n)
{
	float tong = 0;
	for(int i = 1; i <= n; i++)
	{
		float tong2 = 1;
		for(int j = 2; j <= i; j++)
		{
			tong2 += j;
		}
		tong += 1.0/tong2;
	}
	return tong;
}
int main()
{
	int n;
	printf("\nNhap n: "); scanf("%d", &n);

	printf("\nDe quy(%d) = %f", n, deQuy(n));
	printf("\nDe quy duoi(%d) = %f", n, deQuyDuoi(n));
	printf("\nKhu de quy(%d) = %f", n, khuDeQuy(n));

	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
Line 17: error: conio.h: No such file or directory
In function 'deQuy':
Line 9: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 16: error: expected ';', ',' or ')' before '=' token
In function 'khuDeQuy':
Line 31: error: 'for' loop initial declaration used outside C99 mode
Line 34: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: