C + + brush question--2707: Prime and factor

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.