You are here:

C++/c++ primes numbers

Advertisement


Question
one of my programs ish to display the number of primes bewteen two given numbers inputed by the users.

for example;

enter a starting number:50
enter a endling nunber:59
 numbers of primes found in the range: 2


this ish what ii have so far::

/* primes in range chapter4ex11bb /*

#include<iostream.h>
int main()
{
int a,b;
int c;
int d,e;
int f;
int g;


int i;
cout<<"enter a starting number:";
cin>>i;

int j;
cout<<"enter an ending number: ";
cin>>j;

g=0;

if(i<0)
{i=i*-1;}

if(j<0)
{j=j*-1;}

if(i<j)
{
 a=i;
 b=j;
}
else
{
 a=j;
 b=i;
}


for(c=a;c<=b;c++)
{ f=0;
for(d=0;d<=50;d++)
{
for(e=0;e<=50;e++)
{
if(c==(d*e))
{
f=f+1;

}
}
}
if(f==2)
{
g=g+1;
cout<<"
"<<c;
}
}

cout<<"
There are "<< g <<" prime numbers between "<< a <<" and "<< b;

}

Answer
Hi,

This is because the range in the for loop is set to 50; But, I am not sure about this method. If you try dividing the each number after 10 by the following numbers( 2,3,4,5,.. 9) and check for the remainder. Then you will not have the above problem.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


rakshitha

Expertise

Difficult questions related to C .

Experience

C ,vc ,opc ,windows CE, ActiveX

Education/Credentials
Masters in electrical engineering

©2016 About.com. All rights reserved.