8.
package com.company;
import java.util.Scanner;
public class EX_02 {
//九九乘法表
public static void main(String[] args) {
int i,j;
for (i = 1; i <= 9; i++) {
for ( j = 1; j <= i; j++) {
System.out.print(j + " * " + i + " = " + (i * j) + "\t");
}
System.out.println();
}
}
}
12.
package com.company;
public class EX_03 {
public static void main(String[] args) {
//两个数的最大公约数和最小公倍数
int num1 = 24;
int num2 = 36;
int gcd = calculateGCD(num1, num2);
int lcm = calculateLCM(num1, num2);
System.out.println("最大公约数:" + gcd);
System.out.println("最小公倍数:" + lcm);
}
// 最大公约数
public static int calculateGCD(int a, int b) {
if (b == 0) {
return a;
} else {
return calculateGCD(b, a % b);
}
}
// 最小公倍数
public static int calculateLCM(int a, int b) {
int gcd = calculateGCD(a, b);
return (a * b) / gcd;
}
}
13.
package com.company;
public class EX_04 {
//1000以内的完数
public static void main(String[] args) {
System.out.println("1000以内的完数有:");
for (int i = 2; i <= 1000; i++) {
if (isPerfectNumber(i)) {
System.out.println(i);
}
}
}
// 判断是否为完数
public static boolean isPerfectNumber(int num) {
int sum = 1;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
sum += i;
if (i != num / i) {
sum += num / i;
}
}
}
return sum == num;
}
}