题目内容:
编写程序,读入一个整数,然后以升序显示它的所有最小因子,并对这些因子进行求和输出。
输入格式:
整数,number为输入的整数, 数组factor存储因子
输出格式:
System.out.print(number + “'s factor series:”);
System.out.print(factor[i] + ", ");
System.out.println("\nthe sum of factors is " + sum);
输入样例:
100
输出样例:
100’s factor series:2, 2, 5, 5,
the sum of factors is 14
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//System.out.println("Enter number:");
int n1 = input.nextInt();
int number = n1;
int d = 2; //d为因子
int s = 1;
int sum=0;
int flat = -1;
int factor[] = new int[9];
while(d<=n1){
if(n1%d==0){
flat++;
//System.out.println("这个数的因子:"+d);
sum += d;
factor[flat] = d;
s=n1/d; //s为商
n1=s; //重新赋值
continue;
}
d++;
}
System.out.print(number + "'s factor series:");
for(int i=0;i<flat+1;i++) {
System.out.print(factor[i] + ", ");
}
System.out.println("\nthe sum of factors is " + sum);
}
}