Description
Enter any integer n from the keyboard, and if n is not a prime number, all the factors of n are computed (excluding 1), for example, 16, output 2,4,8; otherwise output "It is a prime number."
The judging primes and the factors are used to complete the function respectively.
Input
Any integer n
Output
The factor or prime mark of the number.
Sample Input
16
Sample Output
2
4
8
/* Copyright (c) 2014, Yantai University School of Computer * All rights reserved. * File name: Test.cpp * Chen Dani * Completion Date: June 1, 2015 * version number: v1.0 * * #include <iostream> #include <cmath>using namespace s Td;int is_prime (int n) { int r; if (n==1) return 0; else {for (r=2; r<=sqrt (n), ++r) if (n%r==0) break ; if (R>SQRT (n)) return 1;} } void Yinzi (int n) { int i; for (i=2;i<n;i++) if (n%i==0) Cout<<i<<endl;} int main () { int flag,n; int is_prime (int); cin>>n; Flag=is_prime (n); void Yinzi (int n); if (flag==1) cout<< "It is a prime number." <<endl; else Yinzi (n); return 0;}
Learning experience: feel that they practice some small problems, is very useful, previously unclear function knowledge, in the process of doing these small problems, has solved a lot of it. Efforts to pay will be rewarded, as long as they insist, wait until the arrival of the harvest time. Keep trying!!
C + + brush question--2707: Prime and factor