C language programming> twenty-fifth week ⑥ Please add the function fun. The function of this function is to count the number of prime numbers less than or equal to x (x>2), and the number of prime numbers is returned as the function value.

Example: Please add the function fun. The function of this function is to count the number of prime numbers less than or equal to x (x>2), and return the number of prime numbers as the function value.

例如,输入x=20,结果:2,3,5,7,11,13,17,19。
请勿改动主函数main与其他函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。

代码如下:

#include<stdio.h>
int fun(int x)
{
    
    
	int i,j,count=0;
	printf("\nThe prime number between 2 to %d\n",x);
	for(i=2;i<=x;i++)
	{
    
    
		for(j=2;j<i;j++)
			if(i%j==0)
				break;
		if(j>=i)
		{
    
    
			count++;
			printf(count%15?"%5d":"\n%5d",i);
		}
	}
	return count;
}
main()
{
    
    
	int x=20,result;
	result=fun(x);
	printf("\nThe number of prime is:%d\n",result);
}

The output running window is as follows:
Insert picture description here
other exercises this week

C language programming column

C language programming> Twenty-fifth week① In a given program, the function of the function fun is to insert a "*" after each non-digit character in the string pointed to by the formal parameter s.

C language programming> Twenty-fifth week② In the following program, the function of the function fun is to convert uppercase letters to the fifth letter after the corresponding lowercase letters. If it is a lowercase letter, it is v~z, which reduces the value of the lowercase letter. 21. The converted lowercase letters are returned as the function value.

C language programming> Twenty-fifth week ③ In the following given program, the function of the function fun is to determine whether a triangle can be formed according to the input three side lengths (integer values); whether the formation is an equilateral triangle or wait Waist triangle. If the equilateral triangle function can be formed, it returns 3, if the isosceles triangle function can be formed, it returns 2, if the triangle function can be formed, it returns 1, and if the triangle function cannot be formed, it returns 0.

C language programming> twenty-fifth week ④ Please add the fun function, the function of this function is to find the number that can divide k and is an even number, save these numbers in the array a, and output from large to small.

C language programming> twenty-fifth week ⑤ The function of the following given program is: read in an English text line, change the first letter of each word in it to uppercase, and then output this text line (here "word" Is a string separated by spaces).

C language programming> twenty-fifth week ⑥ Please add the function fun. The function of this function is to count the number of prime numbers less than or equal to x (x>2), and the number of prime numbers is returned as the function value.

C language programming> Twenty-fifth week⑦ The function of the function fun in the following given program is: calculate m!.

C language programming> Twenty-fifth week ⑧ The function of the function fun in the following given program is to take out the even numbers in each digit of the long integer in turn, form a new number and place it in b. The high position is still high, and the low position is still low.

越努力越幸运!
加油,奥力给!!!

Guess you like

Origin blog.csdn.net/qq_45385706/article/details/113093922