100-200 all prime numbers sum program code (two versions) _java

Source: Internet
Author: User

Primes are prime numbers, which are divisible by 1 and cannot be divisible by other numbers.

Java programs are:

Copy Code code as follows:

public static void Main (string[] args) {
The program prints out all the primes from 100 to 200 and sums them up;
int Sum = 0;

for (int i = <= i i++) {
Boolean flag = true;
/* Built-in A For loop, the effect is 100 to 200 of each number, from 21 straight to its own, if equal to 0, then do not belong to the prime, the flag to false*/
for (int j = 2; J <= I-1; j) {
if (i% j = 0)
Flag = false;
}
if (flag) {//Only if flag is true, this is the place to go.
sum = sum + i;//each time the loop lets sum add this prime number I and then reassign to sum
System.out.print (i+ ",");
}
}
System.out.println ("The sum of all prime numbers is:" +sum);
}

The version written by the user is:

Copy Code code as follows:

#include <iostream.h>

The program prints out all the primes from 100 to 200 and sums them up;


void Main ()
{


All prime numbers between cout<< "100-200 are:" <<endl; /* This is the prompt statement, no practical effect;

int k=0; An integer variable k is defined in the main program for storing and downloading all prime numbers, and the default value is 0;

/* The following is a double for loop, the outside loop starts from 100, steps 1, increments to 200, to determine whether each of the numbers is prime number.
for (int i=100;i<=200;i++)
{
int M=0;/*m is a counter, its role is very important, each cycle, it will automatically clear zero, do the next count of preparation.
/* Built-in A For loop, the effect is from 100 to 200 of each number, from 11 straight to its own, such as the first number 100, starting from 1, has been
Except to it itself * *
for (int a=1;a<=i;a++)
{
/* Each addition, if the remainder is 0, then the value of the counter m from the increase 1*/
if (i%a==0) m++;

}
/* After a loop, the results come out, if the value of M is 2, which means that this number can only be divisible by 1 and itself,
This is nuclei. Then the number is printed out, followed by a k+=i; statement, which is to put each of the prime
and assign to K itself in order to find the and/or of these prime numbers.

if (m==2) {cout<<i<< ""; k+=i;}

}

cout<<endl;

cout<< "The and of these prime numbers is:" <<k<<endl;/* here is the number of all the prime numbers obtained, that is, the final value of k * *
}

All prime numbers are: 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199

Sum of all prime numbers: 3167

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.