The C++ Program to Find Prime Numbers between 1 to 100

Hello friends, below is the C++ Program by me to find the Prime Numbers between 1 to 100 using Sieve of Eratosthenes. The program would be common but please focus on the Logic.

content
C++ Program:-

/*
Following C++ program finds prime numbers from 1 to 100 using Method of Sieve of Eratosthenes 
You can modify the program according to your needs, but the general logic remains same.
->By Deep C. Patel 
*/

#include<iostream>
#include<conio.h>
#define MAX 100      //you can increase the limit more than 100 to get more prime numbers

using namespace std;

int main()
{
    int i=0, flag=0, num[MAX], primeNo[MAX], k=0, count=2;
 
    for(i=0;i<MAX;i++)
    {
        num[i]=i+1;
    }

    cout<<"\n\nThe Array has Elements as Shown Below:-\n";
 
    for(i=0;i<MAX;i++)
    {
        cout<<"\n"<<num[i];
    }
 
    primeNo[k]=2;    //Declaring First Prime Number 2
    k++;
 
    while(count<=100)
    {
        flag=0;
  
        for(i=0;i<k;i++)
        {
            if(!(num[count]%primeNo[i])) //checking modulo of stored prime no and the current Number
            {
                flag++;      //If modulo !=0 flag will be incremented
            }
        }
  
        if(flag==0)     //If flag not incremented then current number is Prime
        {
            primeNo[k]=num[count];
            k++;
        }
  
        count++;

    }

    cout<<"\n\nThe Prime Numbers are:-\n";

    for(i=0;i<k;i++)
    {
        cout<<"\n"<<primeNo[i];
    }

    cout<<"\n\nThank You";

    getch();
 
    return 0;
}


Download Link:-

https://drive.google.com/file/d/0B36eSzxwoMyUR0NtTmJuM2dCZU0/view?usp=sharing

For any Queries about any logic or anything you are free to ask me any questions through comments.

Thank You,

Happy New Year 2015 to all of you and Good Bye

Comments

Popular posts from this blog

Programming Paradigms

How to Make Sequence Detectors(Finite State Machines)

4 bit ALU-Verilog Code