Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

BÀI TẬP BUỔI 2

1. Viết chương trình tính ước số chung lớn nhất của hai số nguyên dương a và b
2. import java.util.Scanner;
3.
4. public class UCLN {
5. public static void main(String[] args) {
6. Scanner scanner = new Scanner(System.in);
7.
8. System.out.print("Nhập số nguyên dương a: ");
9. int a = scanner.nextInt();
10. System.out.print("Nhập số nguyên dương b: ");
11. int b = scanner.nextInt();
12.
13. int ucln = timUCLN(a, b);
14.
15. System.out.println("Ước số chung lớn nhất của " + a + " và " + b + " là: " +
ucln);
16.
17. scanner.close();
18. }
19. public static int timUCLN(int a, int b) {
20. while (b != 0) {
21. int temp = b;
22. b = a % b;
23. a = temp;
24. }
25. return a;
26. }
27.}
28.Viết chưng trình tính
𝑥 𝑥2 𝑥3 𝑥𝑛 𝑥𝑖
𝑃𝑛 = 1 + + + +. . . . . + = ∑𝑛𝑖=0 (Đã có bài mẫu)
1! 2! 3! 𝑛! 𝑖!
import java.util.Scanner;
public class TinhPn {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Nhập giá trị của x: ");
double x = scanner.nextDouble();
System.out.print("Nhập giá trị của n: ");
int n = scanner.nextInt();
double result = tinhPn(x, n);
System.out.println("Giá trị của Pn là: " + result);

scanner.close();
}
public static double tinhPn(double x, int n) {
double pn = 1.0; // Khởi tạo Pn với giá trị ban đầu là 1
for (int i = 1; i <= n; i++) {
pn += Math.pow(x, i) / tinhGiaiThua(i);
}
return pn;
}
public static int tinhGiaiThua(int n) {
if (n == 0 || n == 1) {
return 1;
}
int giaiThua = 1;
for (int i = 2; i <= n; i++) {
giaiThua *= i;
}
return giaiThua;
}
}
𝑛
𝑥2 𝑥4 𝑥6 𝑛
𝑥 2𝑛 𝑖
𝑥 2𝑖
𝑄𝑛 = 1 − + − +. . . . . +(−1) = ∑(−1)
2! 4! 6! (2𝑛)! (2𝑖)!
𝑖=0
import java.util.Scanner;
public class TinhQn {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Nhập giá trị của x: ");
double x = scanner.nextDouble();
System.out.print("Nhập giá trị của n: ");
int n = scanner.nextInt();
double result = tinhQn(x, n);
System.out.println("Giá trị của Qn là: " + result);
scanner.close();
}
public static double tinhQn(double x, int n) {
double qn = 1.0;

for (int i = 1; i <= n; i++) {


double term = Math.pow(-1, i) * Math.pow(x, 2*i) / tinhGiaiThua(2*i);
qn += term;
}
return qn;
}
public static int tinhGiaiThua(int n) {
if (n == 0 || n == 1) {
return 1;
}
int giaiThua = 1;
for (int i = 2; i <= n; i++) {
giaiThua *= i;
}
return giaiThua;
}
}
𝑛
𝑥3 𝑥5 𝑥7 𝑛
𝑥 2𝑛+1 𝑖
𝑥 2𝑖+1
𝑆𝑛 = 𝑥 − + − +. . . . . +(−1) = ∑(−1)
3! 5! 7! (2𝑛 + 1)! (2𝑖 + 1)!
𝑖=0
import java.util.Scanner;
public class TinhSn {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Nhập giá trị của x: ");
double x = scanner.nextDouble();
System.out.print("Nhập giá trị của n: ");
int n = scanner.nextInt();
double result = tinhSn(x, n);
System.out.println("Giá trị của Sn là: " + result);
scanner.close();
}
public static double tinhSn(double x, int n) {
double sn = x; // Khởi tạo Sn với giá trị ban đầu là x
for (int i = 1; i <= n; i++) {
double term = Math.pow(-1, i) * Math.pow(x, 2*i+1) / tinhGiaiThua(2*i+1);
sn += term;
}
return sn;
}
public static int tinhGiaiThua(int n) {
if (n == 0 || n == 1) {
return 1;
}
int giaiThua = 1;
for (int i = 2; i <= n; i++) {
giaiThua *= i;
}
return giaiThua;
}
}

You might also like