给定数字N,任务是打印从1到N的质数。
例子:
Input: N = 10
Output: 2, 3, 5, 7
Input: N = 5
Output: 2, 3, 5
方法:
- 首先,将数字N作为输入。
- 然后使用for循环将数字从1迭代到N
- 然后检查每个数字是否是质数。如果它是质数,请打印它。
下面是上述方法的实现:
C++
// C++ program to display first N Prime numbers
#include
using namespace std;
// Function to print first N prime numbers
void print_primes_till_N(int N)
{
// Declare the variables
int i, j, flag;
// Print display message
cout << "\nPrime numbers between 1 and "
<< N << " are:\n";
// Traverse each number from 1 to N
// with the help of for loop
for (i = 1; i <= N; i++) {
// Skip 0 and 1 as they are
// niether prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell
// if i is prime or not
flag = 1;
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is not prime
if (flag == 1)
cout << i << " ";
}
}
// Driver code
int main()
{
int N = 100;
print_primes_till_N(N);
return 0;
}
Java
// Java program to display
// first N Prime numbers
class GFG
{
// Function to print first N prime numbers
static void print_primes_till_N(int N)
{
// Declare the variables
int i, j, flag;
// Print display message
System.out.println("Prime numbers between 1 and "
+ N + " are:");
// Traverse each number from 1 to N
// with the help of for loop
for (i = 1; i <= N; i++)
{
// Skip 0 and 1 as they are
// niether prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell
// if i is prime or not
flag = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is not prime
if (flag == 1)
System.out.print(i + " ");
}
}
// Driver code
public static void main (String[] args)
{
int N = 100;
print_primes_till_N(N);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to display first N Prime numbers
# Function to print first N prime numbers
def print_primes_till_N(N):
# Declare the variables
i, j, flag = 0, 0, 0;
# Print display message
print("Prime numbers between 1 and ",
N , " are:");
# Traverse each number from 1 to N
# with the help of for loop
for i in range(1, N + 1, 1):
# Skip 0 and 1 as they are
# niether prime nor composite
if (i == 1 or i == 0):
continue;
# flag variable to tell
# if i is prime or not
flag = 1;
for j in range(2, ((i // 2) + 1), 1):
if (i % j == 0):
flag = 0;
break;
# flag = 1 means i is prime
# and flag = 0 means i is not prime
if (flag == 1):
print(i, end = " ");
# Driver code
N = 100;
print_primes_till_N(N);
# This code is contributed by Rajput-Ji
C#
// C# program to display
// first N Prime numbers
using System;
class GFG
{
// Function to print first N prime numbers
static void print_primes_till_N(int N)
{
// Declare the variables
int i, j, flag;
// Print display message
Console.Write("Prime numbers between 1 and " +
N + " are:\n");
// Traverse each number from 1 to N
// with the help of for loop
for (i = 1; i <= N; i++)
{
// Skip 0 and 1 as they are
// niether prime nor composite
if (i == 1 || i == 0)
continue;
// flag variable to tell
// if i is prime or not
flag = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
// flag = 1 means i is prime
// and flag = 0 means i is not prime
if (flag == 1)
Console.Write(i + " ");
}
}
// Driver code
public static void Main (String[] args)
{
int N = 100;
print_primes_till_N(N);
}
}
// This code is contributed by Rajput-Ji
输出:
Prime numbers between 1 and 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97