Your Ad Here

Wednesday, February 25, 2009

Write a C++ program to generate all the prime numbers between 1 to n, where n is a value supplied by the user.

#include(iostream.h) // place this '<' & '>' instead of '(' & ')' before iostream.h
#include(conio.h)
void main( )
{
int n, x, flag,ct;
clrscr( );
cout<<"Enter the n value:"; cin>> n;
cout<<"Prime Numbers:";
for( ct=1; ct<=n; ct++)
{
x=2; flag=0;
while(x<=ct/2)
{
if(ct%x==0) { flag=1; break; }
x++;
}
if(flag==0)
cout<< ct;
}
getch( );
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

4 comments:

  1. if it is possible to write a program to generate perfect numbers?

    ReplyDelete
  2. could you explain the logic of flag in it? and the loop also

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  3. could you explain the logic of flag in it? and the loop also

    ReplyDelete