Pascal programming problem

2007-04-05 5:44 am
Use " If ... then ... else " statement ,
write a program to find the prime factors (from 2 to 51) of an integer.

Sample output :
please input a number : 12
2 3 are the prime factors (from 2 to 51) of 12.

I don't know how to do !!
Can anyone help me ?

回答 (1)

2007-04-12 4:29 am
✔ 最佳答案
I don't write pascal but i write C++

The logic should be the same and the syntax should be relatively easy to understand.

void prime(int input)
{
int newnum = input;
int factor = 2;// first prime factor to check is 2.

while (factor * factor <= newnum)
{
if (newnum % factor == 0) //check if the number can be divided by the factor
{
cout << factor //print out the factor
newnum = newnum/factor;
}
else
{
factor++; // try the next possible factor if the number cannot be divided
}
}
}


收錄日期: 2021-05-03 12:26:44
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070404000051KK04594

檢視 Wayback Machine 備份