Custom function outputs all prime numbers within 0-100
In the front of my blog, I used a loop to output all prime numbers within 0-100, but in the previous blog, I explained in detail the difference between passing values and passing addresses, so I optimized this code. Here I use self Define the function, you can simply experience the address transfer.
#include<stdio.h>
int IsPrimeNum(int num)
{
for (int i = 2; i < num; ++i)
{
if (num%i == 0)
{
return 0;//是素数返回1,不是素数返回0
}
}
}
int main()
{
int num = 0;
while (num <= 100)
{
int x=IsPrimeNum(num);//调用自定义函数IsPrimeNum
if (x != 0)
{
printf("%5d", num);
}
++num;
}
return 0;
}
operation result: