当前位置:网站首页>Find all primes less than or equal to Lim, store them in AA array, and return the number of primes

Find all primes less than or equal to Lim, store them in AA array, and return the number of primes

2022-06-26 16:45:00 Muzi..

#include<stdio.h>
#include<stdlib.h>
#define MAX 100
int fun(int lim,int aa[MAX]);
int main()
{
    
    int limit,i,sum;
	int aa[MAX];
	printf(" Enter a number :");
	scanf("%d",&limit);
	sum=fun(limit,aa);
	for(i=0;i<sum;i++)
	{
    
		 if(i%10==0&&i!=0)
		 	 printf("\n");
		 printf("%5d",aa[i]);
	}
}
int fun(int lim,int aa[MAX])
{
    
	 int i,j,k=0;
	 for(i=2;i<=lim;i++) 
	 	{
    
		   for(j=2;j<i;j++)
			 {
    
			   if(i%j==0)
	 	 	  	 break;
	 	     }
	 	 	   if(j>=i)
			  {
    
			    aa[k++]=i;
	       	}
		}
	 return k;
}
原网站

版权声明
本文为[Muzi..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170506477977.html