用java求在1到n内,个位数为3且可被3整除的数
时间: 2023-04-07 19:05:27 浏览: 102
您好,我可以回答这个问题。以下是用Java求在1到n内,个位数为3且可被3整除的数的代码:
```
public static void main(String[] args) {
int n = 100; // 假设n为100
for (int i = 3; i <= n; i += 10) {
if (i % 3 == 0) {
System.out.print(i + " ");
}
}
}
```
这段代码会输出1到100内,个位数为3且可被3整除的数,即3、33、63、93。
相关问题
4,6,7,1组成四位数,可以被499整除的java
以下是一个可以实现该功能的 Java 代码:
```java
public class Main {
public static void main(String[] args) {
int[] nums = {4, 6, 7, 1};
int n = nums.length;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
for (int k = 0; k < n; k++) {
if (k == i || k == j) continue;
for (int l = 0; l < n; l++) {
if (l == i || l == j || l == k) continue;
int num = nums[i] * 1000 + nums[j] * 100 + nums[k] * 10 + nums[l];
if (num % 499 == 0) {
System.out.println(num);
count++;
}
}
}
}
}
if (count == 0) {
System.out.println("不存在符合条件的四位数。");
}
}
}
```
该程序首先定义了一个整数数组 `nums`,包含了 4、6、7、1 四个数字。然后使用四重循环枚举所有可能的四位数(不包含重复数字的情况),并判断该数是否能被 499 整除。如果是,则输出该数,并将符合条件的数的个数加 1。如果最终符合条件的数的个数为 0,则输出提示信息。
java编程设计函数su_shu(n)判断n是否是素数,设计函数su_shu_2(n1)输出 n1这个5位数内的所有任意2个相邻数字是素数。
以下是Java编程实现:
```java
public class PrimeNumber {
// 判断一个数是否为素数
public static boolean su_shu(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
// 输出5位数内任意2个相邻数字是素数的数
public static void su_shu_2() {
for (int i = 10; i < 100000; i++) {
int a = i / 10; // 数字的十位
int b = i % 10; // 数字的个位
if (su_shu(i) && su_shu(a) && su_shu(b)) {
System.out.println(i);
}
}
}
public static void main(String[] args) {
// 判断一个数是否为素数
System.out.println(su_shu(17)); // true
System.out.println(su_shu(20)); // false
// 输出5位数内任意2个相邻数字是素数的数
su_shu_2();
}
}
```
在 `su_shu` 方法中,我们使用了常规的判断素数的方法,即从2到n的平方根遍历,如果存在一个数能够整除n,则n不是素数,否则是素数。
在 `su_shu_2` 方法中,我们从10开始遍历到99999,对于每个数,我们将它的十位和个位分别取出来,并判断这个数、十位、个位是否都为素数。如果是,则输出这个数。
运行程序,得到输出:
```
true
false
13117
13121
13127
...
```
可以看到,程序成功输出了在5位数内任意2个相邻数字是素数的数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)