Print Prime Numbers b/w Two Given Numbers

  • Comp Sci
  • Thread starter Vishalrox
  • Start date
  • Tags
    C++
In summary, the individual wanted help with a C++ program that would print the number of prime numbers between two user given numbers. They attempted to solve the problem but could not print the correct output. The code they provided had some errors and could be improved by using proper coding practices such as using the "!=" operator and defining variables as local as possible. They were also reminded to use "#include <iostream>" instead of "#include<iostream.h>" as it is not part of standard C++.
  • #1
Vishalrox
20
0
Wanted help in c++ !

Program to print the number of prime numbers between two user given numbers.

MY ATTEMPT TO SOLUTION :
I hv found how to print the prime numbers between two given numbers but couldn't print the number of prime numbers between two given numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
int n,x,i,j;
cout<<"Enter the lower range";
cin>>n;
cout<<"Enter the upper range";
cin>>x;
for(i = n;i<=x;i++)
{
for(j = 2;j<=i-1;j++)
{
if(i % j !== 0)
{
cout<<i;
cout<<"\n";
}
}
}
getch();
}
 
Physics news on Phys.org
  • #2


Vishalrox said:
I hv found how to print the prime numbers between two given numbers

The code you posted just prints a number every time i is found to not have a specific factor.
You still need to do something such as check that i % j !== 0 for all j. This could be done with a boolean external to the innermost loop.

Once you have done this, you may use an integer external to the outer loop to count the number of primes.

Also, iostream.h is not part of standard C++. Instead, please use

Code:
#include <iostream>
 
  • #3


I doubt that this code you presented will even compile. A few comments on the coding (since the problem with the algorithm has already been mentioned):
- I don't think I have ever seen the operator "!==". Are you sure you don't mean "!=" ?
- cout<<"\n" can (and should) in many cases be replaced with cout<<endl. "cout<<i; cout<<"\n";" can be combined in a single line "cout<<i<<endl;".
- You should add the line "using namespace std;" before the start of the main routine.
- I made it a habit to give my variables sensible names, a habit that I could also convince my students of (by simply not giving points for parts of the code that I don't understand :P). Sensible variable names would be n->lower, x->upper, i->candidate, j->divisor. That should make your code much more readable.
- Another related good habit is to define variables as local as possible. In c++ you do not need to specify all variables being used at the start of the routine (you had to do that in C). Rather than writing
Code:
int i;
...
for(i=lower; i<=upper; i+=1) { ...
you can write
Code:
for(int i=lower; i<=upper; i+=1) { ...
Apart from being shorter it also improves the readability of code overall.
 

1. What is the purpose of finding prime numbers between two given numbers?

Finding prime numbers between two given numbers is important in many fields such as cryptography, computer science, and number theory. Prime numbers are the building blocks of integers and have unique properties that make them useful for various mathematical and scientific applications.

2. How do you determine if a number is prime or not?

A number is considered prime if it is only divisible by 1 and itself. To determine if a number is prime, we can use trial division, which involves dividing the number by all integers from 2 to its square root. If the number is not divisible by any of those integers, then it is prime.

3. What is the most efficient algorithm for finding prime numbers between two given numbers?

The most efficient algorithm for finding prime numbers between two given numbers is the Sieve of Eratosthenes. This algorithm involves creating a list of all numbers between the two given numbers, and then eliminating all multiples of each prime number until only the prime numbers are left.

4. Can there be prime numbers between any two given numbers?

Yes, there can be prime numbers between any two given numbers. Prime numbers are infinite and can be found between any two given numbers, no matter how close or far apart.

5. What are some real-world applications of finding prime numbers between two given numbers?

As mentioned before, prime numbers have many applications in fields such as cryptography and computer science. In cryptography, prime numbers are used to generate strong encryption keys. In computer science, prime numbers are used in algorithms for data compression and error correction. Prime numbers also have applications in physics, biology, and other sciences.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top