The "C language" counts the element factor contained in an integer and outputs

Source: Internet
Author: User

#include <stdio.h>
#include <math.h>
int prime (int n) {
int i;
if (n<2) {
return 0;
}
for (I=2;I&LT;=SQRT (n); i++) {
if (n%i==0) {
return 0;
}
}
if (I&GT;=SQRT (n)) {
printf ("Prime:%d\n", N);
return 1;
}

}

void Main () {
int i,n,count=0;
while (1) {
printf ("Please enter an integer:");
scanf ("%d", &n);

for (i=0;i<=n;i++) {
Count+=prime (i);
}
printf ("Integer%d has%d prime factor \ n", N,count);
}
}

Remarks: for (I=2;I<=SQRT (n); i++) {
if (n%i==0) {
return 0;
}
}

In this code, the judge is not a prime number can not be used break, must use return 0, or the following count will be a very strange big numbers! Don't ask me how I know, the lesson of blood!

The "C language" counts the element factor contained in an integer and outputs

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.