C Language Judgment Prime (break statement) (continue statement)

Source: Internet
Author: User

First, judge the prime number

question : Enter a positive integer n from the keyboard to determine whether it is a prime number.

Analysis : Prime is only 1 and the number of its own division, the algorithm is to determine the prime numbers, with i=1,2,3.......,n-1 to remove N, if I can divide n, that n is not prime, stop the test, or continue to test i++, if the number of 2~n-1 can not be evenly divisible by N, Then the output n is a prime number.

Program One

#include <stdio.h>intMain () {inti; intN; scanf ("%d",&N);  for(i=2; i<=n-1; i++)    {        if(n%i==0)        {             Break; }    }         if(i>n-1) printf ("yes!\n"); Elseprintf ("no!\n"); return 0;}

Program Two

#include <stdio.h>intSushu (intN) {    inti; if(n<=1)    {        return 0; }     for(i=2; i<=n-1; i++)    {        if(n%i==0)         Break; }    if(i>n-1)    return 1; Else    return 0;}intMain () {intN; scanf ("%d",&N); if(Sushu (n)) printf ("yes!\n"); Elseprintf ("no!\n"); return 0;}

Cheng

#include <stdio.h>#include<math.h>intSushu (intN) {    inti; if(n<=1)    {        return 0; }     for(i=2; I<=sqrt (n); i++)    {        if(n%i==0)         Break; }    if(i>sqrt (n))return 1; Else    return 0;}intMain () {intN; scanf ("%d",&N); if(Sushu (n)) printf ("yes!\n"); Elseprintf ("no!\n"); return 0;}

Rules for using Break statements

1. The break statement cannot be used in any statement other than the Loop statement and the switch statement

2.break statements are generally used with the IF statement in the loop body

3, in the multi-layer loop, a break statement can only jump to a layer, jump to the outermost layer needs to set the break more than once.

Continue statements

The continue statement acts by skipping the remaining statements in the loop body and forcing the next loop to be executed. The continue statement is used only in loop bodies such as for,while and do-while, usually with an if conditional statement. Used to speed up the loop.

The difference between a continue statement and a break statement

The continue statement ends the loop only, continuing with the next loop, rather than terminating the entire loop execution, and the break statement is the execution of the entire loop.

Output 3-bit integers divisible by 3 and 7

#include <stdio.h>intMain () {inti;  for(i= -; i<=999; i++)    {        if(i%3!=0)Continue; if(i%7!=0)Continue; printf ("%d\n", i); } printf ("\ n"); return 0;}

C Language Judgment Prime (break statement) (continue statement)

Related Article

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.