素数:
public class Prime {
public static void main(String[] args) {
int b = 0;
for (int i = 3; i <= 100; i++) {
int a=2;
for (int n = 2; n < i; n++) {
if ((i % n) != 0) {
a++;
}
}
if (a == i) {
b++;
System.out.print(i + " ");
if ((b % 5) == 0) {
System.out.println();
}
}
}
System.out.println("
共有" + b + "个质数");
}
}
回文:
import java.util.Scanner;
public class Recurrence {
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner reader=new Scanner(System.in
);
String str=reader.next();
int n=str.length();
int m=huiwen(str,n);
if(m==1)
System.out.println("这个字符串是回文字符串!");
else
System.out.println("这个字符串不是回文字符串!");
}
public static int huiwen(String str,int n) {
int a,b,j=0;
char c1,c2;
a=str.length()-n;
b=str.length()-(a+1);
c1=str.charAt(a);
c2=str.charAt(b);
if(c1==c2||a==b) j=1;
if(a!=b&&a<b&&j==1)
huiwen(str,n-1);
return j;
}
}