Novice C language learning journey(2)
This is a code of to solve the prime numbers.
But it has a problem,the print results is not prime numbers entirely.And it print many repeat numbers.The code as follows:
#include"math.h"
#include"stdio.h"
int main(void){
int i,j,a,n;
n=0;
for(i=101;i<200;i+=2){
if(n%10==0)
printf("\n");
j=sqrt(i);
for(a=2;a<=j;a++)
if(i%a==0)
break;
else {
printf("%d ",i);
n++;
}
}
return 0;
}
But no long after.I found the reason.the problem is this statement of "else".
"i" variable can be devisible by some "a" variable when a<=j.But the statement is "else" in the following statement.So it print i variable in console.But the value of i variable at the moment is not a prime numbers.So the modified code is as follows:
#include"math.h"
#include"stdio.h"
int main(void){
int i,j,a,n;
n=0;
for(i=101;i<200;i+=2){
if(n%10==0)
printf("\n");
j=sqrt(i);
for(a=2;a<=j;a++)
if(i%a==0)
break;
if(a>j) {
printf("%d ",i);
n++;
}
}
return 0;
}
Welcome sky-999!
eSteem is mobile and desktop application that improves your experience on Steem.
Download Android and iOS apps. Surfer Desktop application that helps you to gain new followers and stay connected with your friends, unique features - notifications, bookmarks, favorites, drafts, and more.
We reward our users with encouragement upvotes as well as ESTM token.
Learn more: https://esteem.app
Join our discord: https://discord.me/esteem
Welcome to steemit @sky-999.
Welcome the new steemians. Have a great day!