#include<iostream>
using namespace std;
int main()
{
    int test;
    cin>>test;
    while(test--)
    {
    int start,end,m;
    cin>>start;
    cin>>end;
    bool arr[end];
    for(int i=0;i<end;i++)
    arr[i]=true;
    for(int i=2;(i*i)<=end;i++)
    {
            for(int p=2;(p*i)<=end;p++)
            {
                    m=(p*i-1);
                    arr[m]=false;
                    }
                    }
                    for(int i=start-1;i<=end;i++)
                    {
                            if(arr[i]!=0&&i!=0)
                            cout<<i+1<<endl;
                            }
                            }
                            return 0;
                            }

What is the question ?
If you want an C++ algorthm to generate prime numbers just googleit. There are alot of examples.

Question is why it is giving run time error on spoj even though it is working fine on my machine and giving correct output..Plesae tell me why it is giving run time error and what could be the solution..

You need to change all of your comparisons in your loops from <= end to < end. The reason for this is if i or p equals end then you are one past the end of the array and reading data that my not belong to you and thus returning the runtime error

I test your code, work fine for me in CodeBlocks.
I test the .exe file too, work ok, dont give me runtime error.

I attachment you have the exe file with your program.

Some one please try to run it on spoj..And see what happens..I am not getting why it is still giving runtime error.

I dont know what kind of compiler use SPOJ. Their compiler is the problem.
By the other way, i looked over your code... isn't a very good solution.
I propose you to use it the next:

#include<iostream>
#include <conio.h>

using namespace std;

 int main()
 {
    int test;
    int start, end;
    int j;
    bool isprime;
    cin>>test;
    while(test--)
    {
        cin>>start;
        cin>>end;
        for(int n = start; n < end; n++)
        {
            isprime = 1;
            for (j = 2; j <= n/2; j++)
            {
                if (n % j == 0)
                {
                    isprime = 0;
                    break;    
                }
            }
            if(isprime)
            {
                cout<<n<<"\n";
            }                
        }
    }
    _getch();
    return 0;
}

There are several issues with this program that will result in problems.

First off the cause of the error is likely to be the lines:

cin >> end;
bool arr[end];

You just can't do that in standard C++. The array is sized at compile time not runtime. [I know several compilers actually work with this but ... ]

The size of arr could be anything so fix it like this : [Assuming that you don't want to use a resizable container like std::vector]

int maxTestNumber
std::cin >> maxTestNumber;
if (maxTestNumber<0 || maxTestNumber>10000)
  {
     std::cout<<"Test number out of sane range "<<std::endl;
     return -1;
  }
bool* arr=new bool[maxTestNumber];
// ... Your code here 

// REMEMBER :: memory allocated with new should be deleted
delete [] arr;
return 0;
}

Next not actual an error, BUT if you intend to put `using namespace std;``
into your code. Do you really want to be using variable names like "end" which is just one character away from endl and a whole pile of pain to find the error.

Next: you have the last loop going from i=start-1 to AND including end. Thus on line 25 you do if (arr[i]!=0 && i!=0) Unfortunately, you have just accessed arr +1 past the last member of the array!

After that you shoudl be in a postion to fix your algorithm..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.