text: Program to print prime numbers from 1 to N . | C ++ program to display first N Prime numbers ; Function to print first N prime numbers ; Declare the variables ; Print display message ; Traverse each number from 1 to N with the help of for loop ; Skip 0 and 1 as they are neither prime nor composite ; flag variable to tell if i is prime or not ; flag = 1 means i is prime and flag = 0 means i is not prime ; Driver code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void print_primes_till_N ( int N ) { int i , j , flag ; cout << " Prime numbers between 1 and " << N < < " ▁ are : STRNEWLINE " ; for ( i = 1 ; i <= N ; i ++ ) { if ( i == 1 i == 0 ) continue ; flag = 1 ; for ( j = 2 ; j <= i / 2 ; ++ j ) { if ( i % j == 0 ) { flag = 0 ; break ; } } if ( flag == 1 ) cout << i << " ▁ " ; } } int main ( ) { int N = 100 ; print_primes_till_N ( N ) ; return 0 ; }