C language function exercises explained

We have just shared important knowledge points about C language functions, so let’s strike while the iron is hot and solve some function exercises.

  1. Write a function to determine whether a number is prime.

Without further ado, let’s just look at the code.

#include<stdbool.h>
bool is_prime(int n)
{
    
    
	//拿2~sqrt(n)之间数字试除
	int j = 0;
	for (j = 2; j <= sqrt(n); j++)
	{
    
    
		if (n % j == 0)
			return false;
	}
	return true;//是素数
}

int main()
{
    
    
	//打印100~200之间的素数
	int i = 0;
	int count = 0;
	for (i = 101; i <= 200; i+=2)
	{
    
    
		//判断i是否是素数?
		if (is_prime(i))
		{
    
    
			count++;
			printf("%d ", i);
		}
	}
	printf("\ncount = %d\n", count);
	return 0;
}

It should be noted here that the return true or false must be added to the header file <stdbool.h>. The i of the main function is called to our custom function by passing value. If i cannot be divided by the number from 2 to the square root of i, then it is a prime number. When the if judgment statement in the return true to the main function is true, count and print it, and then enter the next loop.
The printed result is as shown in the figure

  1. Write a function to determine whether a year is a leap year.
bool is_leap_year(int y)
{
    
    
	if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
		return true;
	else
		return false;
}


int main()
{
    
    
	int y = 0;
	int count = 0;
	for (y = 1000; y <= 2000; y++)
	{
    
    
		//判断y是否是闰年
		if (is_leap_year(y))
		{
    
    
			count++;
			printf("%d ", y);
		}
	}
	printf("\ncount = %d\n", count);

	return 0;
}

Here we still use the method of the previous question, making judgments in the custom function, counting and printing in the main function, and we still use the library function in <stdbool.h>. The key point in completing this question is that we must be clear about the judgment principle of leap year. The judgment principle of leap year is that the year is divisible by 4 and not divisible by 100 or the year is divisible by 400, then this question will be easily solved.

Insert image description here
So here we can clearly see the result of this question.

3. Write a function that will increase the value of num by 1 every time this function is called.

void Add(int* p)
{
    
    
	*p = *p + 1;
}

int main()
{
    
    
	int num = 0;
	Add(&num);
	printf("%d\n", num);
	Add(&num);
	printf("%d\n", num);
	Add(&num);
	printf("%d\n", num);

	return 0;
}

Here we use a pointer variable, pass the address of num to p, add 1 to *p in the Add function, and then print it back to the main function.

Insert image description here
Thank you again for your support and attention. The blogger's previous blog may not be of high quality, but with your encouragement and support, the blogger's ability has also been improved. Thank you again for your support. That's all for today's sharing. thank you all.

Guess you like

Origin blog.csdn.net/Lehjy/article/details/132241066