当前位置:网站首页>Various types of questions judged by prime numbers within 100 (C language)

Various types of questions judged by prime numbers within 100 (C language)

2022-07-05 08:15:00 chen_ bx

subject 1: Judge 100 What are the primes within ?


Prime numbers are also called prime numbers , Is greater than 1 Of the natural number , except 1 A natural number that has no other factor than itself .

Use for A nested loop , Traverse

The code is as follows :

#include <stdio.h>

int main()
{
    
	/*  Definition of local variables  */
	int i, j;
	printf("100 Prime numbers within \n");
	for (i = 2; i < 100; i++) // Determine the scope to find 
	{
    
		for (j = 2; j <= i; j++)// Range of determining factors 
			if (i%j ==0) {
    
				break;
			} //  If you find , Is not a prime number 
			else if (j ==i-1)// Until the last divisor , I didn't find , So this number i As a prime number 
			{
    
				printf("%d\t", i);
			} 
	}
	return 0;
}

Output results
 Insert picture description here


subject 2: Enter a number to determine whether it is a prime number ?

#include <stdio.h>

int main()
{
    
	int x = 0;
	int i = 0;
	printf(" Please enter the number of judgments required :");
	scanf_s("%d", &x);
	if (x < 2)
	{
    	
		printf(" Please enter the ratio 1 Large number ");
	}
	else 
	{
    
		for (int i = 2; i < x; i++)
		{
    
			if (x%i == 0) {
    
				printf(" This number is not prime ");
				break;
			}
			else if (i == x - 1)
			{
    
				printf("%d  Prime number ", x);
			}
		}
	}
}



Output results :
 Insert picture description here
 Insert picture description here


原网站

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