Sunday, March 17, 2013

C++ program to find prime numbers in a given range

Write a C++ program to print all the prime numbers with in the given range



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

int main(){
    int num,i,count,n;
    cout << "Enter max range: ";
    cin >> n;
    for(num = 1;num<=n;num++){
         count = 0;
         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }
       
         if(count==0 && num!= 1)
              cout << num << setw(3);
    }
 system("pause");
   return 0;
}

C++ program to find prime numbers in a given range 






---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------



Program in C++ to find the prime number in given range
Write a C++ program to find prime numbers between 1 to 100
Prime numbers in a range - C++
Program to find the prime numbers between a given range
Write a program to print all the prime numbers with in the given range
C++ program to find prime numbers between two numbers
C++ program to find sum of prime numbers
C++ Program to Find Prime Numbers in a given Range

10 comments:

  1. Easiest programme to create prime numbers.

    ReplyDelete
  2. very good programming jobs site www.fresherssoftwarejobs.in

    ReplyDelete
  3. very good programming jobs site www.fresherssoftwarejobs.in

    ReplyDelete
  4. Thank Yu so much sir ! Highly appreciated you for this effortt

    ReplyDelete
  5. Prime number program in C++

    A prime number is a natural number which is greater than 1 and these numbers are only divisible by 1 and itself number. In programming point of view, we check condition number is divisible by other number or not except 1 and number itself. If number is not divisible by other number than that number is prime number.

    ReplyDelete
  6. http://techcpp.blogspot.in/2016/04/program-to-check-whether-given-number.html

    ReplyDelete
  7. To be honest this is not my solution. I wrote this from my head ;)
    The time in which you get a solution, is incomparably shorter.

    #include <array>
    #include <chrono>
    #include <cstdlib>
    #include <iomanip>
    #include <iostream>

    int main(int count, char ** values) {
    auto start = std::chrono::system_clock::now();

    const std::size_t valueMax = 2000000;
    std::array<bool, valueMax + 1> array;
    array.fill(true);
    for (std::size_t index = 1; index <= valueMax; ++index) {
    if (!array[index]) {
    continue;
    }
    // std::cout << index << ":";
    if (index == 1) {
    continue;
    }
    for (std::size_t x = index; x < array.size(); x += index) {
    array[x] = false;
    }
    }

    auto end = std::chrono::system_clock::now();
    std::cout << "\n" << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    return EXIT_SUCCESS;
    }

    ReplyDelete