[ create a new paste ] login | about

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

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

void NhapDuLieu(int &n)
{
	// n >= 1
	do
	{
		printf("\nNhap vao so nguyen duong n (n >= 1): ");
		scanf_s("%d", &n);
		
		if (n < 1)
		{
			printf("\nGia tri n khong hop le. Xin kiem tra lai !");
		}
	} while (n < 1);
}

int TinhCau_a(int n)
{
	int Tong = 0;
	for (int i = 1; i <= n; i++)
	{
		Tong += i;
	}
	return Tong;
}

int TinhCau_b(int n)
{
	int Tong = 0;
	for (int i = 1; i <= n; i++)
	{
		Tong = Tong + i * i;
	}
	return Tong;
}

float TinhCau_c(int n)
{
	float Tong = 0;
	for (int i = 1; i <= n; i++)
	{
		Tong = Tong + (float)1 / i;
	}
	return Tong;
}
int TinhCau_d(int n)
{
	int Tich = 1;
	for (int i = 2; i <= n; i++)
	{
		Tich *= i;
	}
	return Tich;
}
// 1 + 1*2 + 1*2*3 + .... + 1 * 2 * 3 *...* n
int TinhCau_e(int n)
{
	int Tong = 0, Tich = 1;
	for (int i = 1; i <= n; i++)
	{
		Tich = Tich * i;
		Tong = Tong + Tich;
	}
	return Tong;
}

/*
n = 1 => sqrt(2)
n = 2 => sqrt(2 + sqrt(2))
n = 3 => sqrt(2 + sqrt(2 + sqrt(2))) 

S(n) = sqrt(2 + S(n - 1))
*/

float TinhBaiKho(int n)
{
	float S = sqrt((float)2);
	for (int i = 2; i <= n; i++)
	{
		S = sqrt(2 + S);
	}
	return S;
}

int main()
{

	int n;
	NhapDuLieu(n);
	
	// 1 + 2 + 3 +....+ n = (n * (n +1)) / 2
	int Sa = TinhCau_a(n);
	printf("\nSa = %d", Sa);

	int Sb = TinhCau_b(n);
	printf("\nSb = %d", Sb);

	float Sc = TinhCau_c(n);
	printf("\nSc = %f", Sc);

	int Sd = TinhCau_d(n);
	printf("\nSd = %d", Sd);

	int Se = TinhCau_e(n);
	printf("\nSe = %d", Se);


	float S = TinhBaiKho(n);
	printf("\nS = %f", S);
	_getch();
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Line 17: error: conio.h: No such file or directory
Line 5: error: expected ';', ',' or ')' before '&' token
In function 'TinhCau_a':
Line 23: error: 'for' loop initial declaration used outside C99 mode
In function 'TinhCau_b':
Line 33: error: 'for' loop initial declaration used outside C99 mode
In function 'TinhCau_c':
Line 43: error: 'for' loop initial declaration used outside C99 mode
In function 'TinhCau_d':
Line 52: error: 'for' loop initial declaration used outside C99 mode
In function 'TinhCau_e':
Line 62: error: 'for' loop initial declaration used outside C99 mode
In function 'TinhBaiKho':
Line 81: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: