C language implementation of statistical prime and sum code to share _c language

Source: Internet
Author: User

The problem comes from the Pat platform, which is a no-brainer. The title requires the output of the number of characters in a given interval and sums them up. The specific idea is to use the cycle to determine the prime number, the results passed to the control variables, the control variables to determine whether to perform the increase and sum. Of course, it must be noted that 1 is neither prime nor composite.

Here's the code:

Copy Code code as follows:

#include <stdio.h>
int main ()
{
int a=0,b=0;
int n=0,sum=0;
int x=0,i=0;
scanf ("%d%d", &a,&b);
int check=1; Suppose to be prime
for (x=a;x<=b;x++) {
check=1;
for (i=2;i<x;i++) {
if (x%i==0) {
check=0;
Break
//The loop is used to determine if it is a prime number, to iterate over the numbers less than X, and to assign a value of 0 to a variable that is divisible by x, and jump out of the loop directly
}
if (check!= 0 && x!=1) {
n++;
Sum+=x;
//The If is used to determine the check value that was passed before, and to add whether X is 1
}
printf ("%d%d", n,sum);
return 0;
}

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.