C language and c ++: two methods: printing 100 ~ Prime number between 200

Source: Internet
Author: User

C language and c ++: two methods: printing 100 ~ Prime number between 200
Method 1 programming in C language:

#include <stdio.h>#include <math.h>int main(){int i=0;    int count=0;for(i=101;i<=199;i+=2){int j=0;for(j=3;j<=0.5*sqrt(i);j+=2){if(i%j==0){break;}}if(j>0.5*sqrt(i)){count++;printf("%d  ",i);}}printf("count=%d\n",count);return 0;}

 

Output result: 101 103 107 109 113 119 121 127 131 133 137 139 143 149 151 157 161 163 167 169 173 179 181 187 191 193 197 199 count = 28 Press any key to continue with c ++ programming: # include <iostream> # include <cmath> using namespace std; int main () {int I = 0; int count = 0; for (I = 101; I <= 199; I + = 2) {int j = 0; for (j = 3; j <= 0.5 * sqrt (I); j + = 2) {if (I % j = 0) {break ;}} if (j> 0.5 * sqrt (I) {count ++; cout <I <"" ;}} cout <count <endl; return 0;} output result: 101 103 107 109 113 119 121 127 131 133 137 139 143 149 151 157 161 163 167 169 173 179 181 187 191 193 197 28 Press any key to continue method 2 C language programming
#include<stdio.h>#include<math.h>main(){       int i,k,tag,j=0;    for (i=100;i<=200;i++){tag=0;      for(k=2;k<i;k++)        if(i%k==0)tag=1;        if(tag==0){printf("%d ",i); j++;if(j%5==0)printf("\n");}  }}

 

Output: 101 103 107 109 113127 131 137 139 149151 157 163 167 173179 181 191 193 197199 Press any key to continue

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.