``````//I have a problem, i trying to get the total prime numbers for each row on the output.
//I jus getting the total prime numbers from the firs line 1 to 100
// my task is to get the total prime number from 1 to 100, 1001 to 2000.....49001 to 5000
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

bool find_prime(long);
//long primeCount (long x, long y);

int main ()
{
int i, counter =0;
bool test;

for (i =1; i < 1000; i++ )
{

test = find_prime(i);
if ( test == true )
{

counter++;

}

}
cout << "Start" <<setw(6) << "End" << setw(24) << "Number of Primes" << endl;

for (i; i <= 50000; i = i + 1000)

{

cout << i-999 <<setw(10) << i << setw(11) << counter << endl;

}
cout << "Total primes in the first 50 chiliads:" << counter << endl;

cout << "Average number per chiliad:" << counter/1 << endl;
system("pause");
}

bool find_prime (long num)
{
int j;

if(num <= 1)
return false;

for ( j = 2; j*j <= num; j++ )

if (num%j == 0)

return false;
return true;

}``````

``````#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
void TestPrime(int );
void TestPrime(int n)
{
int no=n;
int i,j,p,flag=0,k;
if(no==1)
{
printf("1 is prime");
}
for ( i = 2; i<no && !flag; i++)
{
if(no%i==0)
{
flag=1;
}
}
if(flag==0)
{
cout<<"\nno is"<<i<<"is Prime No";

}
else
{
//cout<<"\nno is"<<i;
// cout<<"\nNot Prime";
}
}
int main()
{
cout<<"Hello";
int i,j,n;
int a[10];
for (i=1; i<100; i++)
{
TestPrime(i);

}
getch();

}``````
``````#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
void TestPrime(int );
int total=0;
void TestPrime(int n)
{
int no=n;
int i,j,p,flag=0,k;
if(no==1)
{

}
for ( i = 2; i<no && !flag; i++)
{
if(no%i==0)
{
flag=1;
}
}
if(flag==0)
{
// cout<<"\nno is"<<i<<"is Prime No";
total++;

}
else
{
//cout<<"\nno is"<<i;
// cout<<"\nNot Prime";
}
}
int main()
{

int i,j,n;
int a[10];
for (i=1; i<100; i++)
{
TestPrime(i);

}
printf("Total Prime no upto 100 is:-> %d",total-1);
getch();

}``````

L33t speak is against the rules, and so is giving away free code. Read this.

``````void TestPrime(int );
void TestPrime(int n)
{``````

Why is the first line there? You don't have to declare the function if you're going to define it right away. Lose the first line.

``````if(no==1)
{
printf("1 is prime");
}``````

I wouldn't call 1 a prime, but there's still somewhat discussion on the subject. Anyway: why didn't you just include the number 1 is this loop:

``````for ( i = 2; i<no && !flag; i++)
{``````

By changing the '2' to '1', the first if statement is obsolete.

``````if(no%i==0)
{
flag=1;
}``````

If this function isn't going to return anything, but only print if it's a prime or not; why not print it here right away? Saves you an extra if statement.

 And don't get me started on your second code

So at the OP: You might want to have a look at the logic from the post above, but ctrl-c ctrl-v is not a good idea

at OP: You need nested loops. One that counts from 0-1000 and one that run the first loop 50 times (50 * 1000 = 50000)

Using your own code, it would be something like:

``````cout << "Start" <<setw(6) << "End" << setw(24) << "Number of Primes\n";
for ( int k = 0; k < 50000; k+=1000)
{
for (i =1; i < 1000; i++ )
{
test = find_prime(i+k);
if ( test == true )
{
counter++;
}
}
cout << i+k-999 <<setw(10) << i+k << setw(11) << counter << endl;
counter = 0;
}``````

You'll have to declare one more var, to hold the total number of primes

To check Wheather a no 'n' is prime or not why not check if 'n' is divided by the prime numbers from 2 to sqrt of the number 'n'?
It would prove better

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.