我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*P3Q16
* Wang Chaoran
* Description:16 (Finding the factors of an integer)
Write a program that reads an integer and displays all its smallest factors.
For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.
*/
import java.util.Scanner;
public class P3Q16
{
public static void main(String[] args)
{
int count=0,i=2;
System.out.println("Please enter an integer: ");
//Create a scanner
Scanner scan =new Scanner(System.in);
//Read in an integer
int input = scan.nextInt();
String output="";
//compute all its smallest factors
while(i<=input/2)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
{
count++;
}
}
if((count==0)&&(input%i==0))
{
while (input%i==0&&input/i!=1)
{
output =output +i+", ";
input=input/i;
}
}
i++;
count = 0;
}
output =output+input+".";
//Display the output
System.out.println("The smallest factors of "+input+" are "+output);
}
}
/*P3Q16
* Wang Chaoran
* Description:16 (Finding the factors of an integer)
Write a program that reads an integer and displays all its smallest factors.
For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.
*/
import java.util.Scanner;
public class P3Q16
{
public static void main(String[] args)
{
int i=2;
System.out.println("Please enter an integer: ");
//Create a scanner
Scanner scan =new Scanner(System.in);
//Read in an integer
int input = scan.nextInt();
String output="";
//compute all its smallest factors
while(i<=input/2)
{
while (input%i==0&&input/i!=1)
{
output =output +i+", ";
input=input/i;
}
i++;
}
output =output+input+".";
//Display the output
System.out.println("The smallest factors of "+input+" are "+output);
}
}
导入论坛查看(48)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing