编程题
编写程序,求1!+2!+……+10!
public static double jc(int m)throws Exception{
if(m<=0) {
throw new Exception("负数无阶乘,请你输入正数");
}
double s=1;
for(int i=1;i<=m;i++){
s=s*i;
}
return s;
}
编写一个程序求100以内的全部素数
static boolean ss(int m) throws Exception{
if(m<=0) {
throw new Exception("素数定义中不包含负数,请你输入正数");
}
if(m==1)
return false;
for(int i=2;i<=m/2;i++){
if(m%i==0){
return false;
}
}
return true;
}
分别用do while和for循环计算1+1/2!+1/3!+……前20项和
public static double jcdsh(int m){
double sum=0;
int a=1;
do{
try {
sum+=1.0/sy2.example3.jc(a);
a++;
}catch(Exception e) {
System.out.println(e.getMessage());
}
//System.out.println(sum);
}while(a!=20);
return sum;
}
一个数如果恰好是它的因子之和,那么这个数称为完数,编写程序,求1000以内的所有完数
public static void main(String[] args) {
// TODO Auto-generated method stub
//1 肯定不是完数
try{
for(int i=2;i<=1000;i++){
if(sy2.t4_2.ss(i)){
// 素数肯定不是完数
}
else{
int sum=0;
for(int j=1;j<=i/2;j++){
if(i%j==0){
sum=sum+j;
}
}
if(sum==i){
System.out.print(sum+" ");
}
}
}
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
编写应用程序,使用for循环语句计算8+88+888+……前10项之和
static double jg(int m,int n) throws Exception{ //m为数字,n为几个
if(m<0||m>9) {
throw new Exception("重复的数字只能为1-9的数字!!!");
}
double t=m;
double sum=0;
for(int i=1;i<=n;i++) {
sum=t+sum;
t=10*t+m;
// System.out.println(t);
}
return sum;
}
编写应用程序输出,1+2+3+……+n<8888的最大正整数
public static void main(String[] args) {
// TODO 自动生成的方法存根
int sum=0;
int i=1;
do {
sum=sum+i;
i++;
}while(sum<8888);
System.out.println("这个最大正整数为:"+(i-1));
}