每日一編C-1

/*判斷101-200之間有多少的素數,並輸出所有素數及其個數*/

#include <stdio.h>
#include <math.h>

int main()
{
	int m,i,k,h,leap;
	
	h=0;
	leap=1;
	
	printf("\n");
	
	for( m = 101; m <= 200; m++)
	{
		k=sqrt(m+1);
		for( i = 2; i <= k; i++ )
			if( m%i == 0 ) 
				{leap=0;break;}
			
			if(leap)  /*內部循環結束,leap 仍然爲1,則m爲素數 */
			{
			printf("-4d", m);
			h++;
			if( h%10 == 0)
				printf("\n");
			}			
		leap = 1;
	}	
	printf("\nThe total is %d",h);
	
	return 0;
				
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章