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 :: c++ finding gcd 
Cpp :: c++ preprocessor operations 
Cpp :: pointer cpp 
Cpp :: c++ exceptions 
Cpp :: c++ auto 
Cpp :: maxheap cpp stl 
Cpp :: vector of vectors of pairs c++ 
Cpp :: c++ region 
Cpp :: map in cpp 
Cpp :: explicit c++ 
Cpp :: template c++ 
Cpp :: heap buffer overflow in c 
Cpp :: dice combinations cses solution 
Cpp :: preorder 
Cpp :: stack class implementation to file unix-style in c++ 
Cpp :: c++ loop trhought object 
Cpp :: bfs sudocode 
Cpp :: constrain function in arduino 
Cpp :: min heap stl 
Cpp :: what is function c++ 
Cpp :: how to concatinate two strings in c++ 
Cpp :: c++ call by value 
Cpp :: has substr c++ 
Cpp :: async multi thread 
Cpp :: educative 
Cpp :: the difference between i++ and ++i 
Cpp :: memcpy in cpp 
Cpp :: c++ main function parameters 
Cpp :: ? c++ 
Cpp :: cuda shared array 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =