Search
 
SCRIPT & CODE EXAMPLE
 

CPP

C++ program that prints the prime numbers from 1 to 1000.

#include <iostream>
#include<cmath>
using namespace std;

int main()
{   cout << "Prime Numbers between 1 and 100 are:
";

    for(int i=2;i<=100;++i) //loop to check for each number in the range

    {   int ctr=0; //to maintain factor count

        for(int j=2;j<=sqrt(i);++j) //checking for factors

        {   if(i%j==0)

                ctr=1; //increasing factor count when found

        }

        if(ctr==0) //checking and printing prime numbers

                cout<<i<<" ";

    }

    return 0;

}
Comment

c++ program to generate all the prime numbers between 1 and n

// C++ program to display Prime numbers till N
#include <bits/stdc++.h>
using namespace std;
 
//function to check if a given number is prime
bool isPrime(int n){
      //since 0 and 1 is not prime return false.
      if(n==1||n==0) return false;
   
      //Run a loop from 2 to n-1
      for(int i=2; i<n; i++){
        // if the number is divisible by i, then n is not a prime number.
        if(n%i==0) return false;
      }
      //otherwise, n is prime number.
      return true;
}
 
 
// Driver code
int main()
{
    int N = 100;
 
    //check for every number from 1 to N
      for(int i=1; i<=N; i++){
          //check if current number is prime
          if(isPrime(i)) {
            cout << i << " ";
          }
    }
 
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: check if character in string c++ 
Cpp :: comparator for priority queue c++ 
Cpp :: c++ binary search 
Cpp :: c++ standard library source 
Cpp :: swapping of two numbers 
Cpp :: create a 2d vector in c++ 
Cpp :: c++ structure 
Cpp :: c++ vector declaration 
Cpp :: c++ loop vector 
Cpp :: Pyramid pattren program in C++ 
Cpp :: cpp pushfront vector 
Cpp :: powershell get uptime remote computer 
Cpp :: how to use cout function in c++ 
Cpp :: palindrome program in c++ 
Cpp :: how to sort vector of struct in c++ 
Cpp :: calculate factorial 
Cpp :: c++ min int 
Cpp :: how to compile opencv c++ in ubuntu 
Cpp :: What is the "--" operator in C/C++? 
Cpp :: convert 2d array to 1d c++ 
Cpp :: cpp class constructor 
Cpp :: no template named vector in namespace std 
Cpp :: c++ vector remove all duplicate elements 
Cpp :: define in cpp 
Cpp :: c++ preprocessor operations 
Cpp :: array of struct in c++ 
Cpp :: visual studio getline not working 
Cpp :: bfs to detect cycle in undirected graph 
Cpp :: cpp execute command 
Cpp :: operator precedence in cpp 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =