Hi
I have seen many topics on prime numbers. But not what I am trying.
I am trying to write a program where the user inputs a number , this number is X prime number. So if they input 10 they want to know the 10th prime so we would display 31.
here is what I thoght would work
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main()
{
int PrimeCounter = 0 ;
int PrimeNumberNeeded ;
bool Isprime ;
Isprime = true ;
cout << "Please enter the Prime Needed: " ;
cin >> PrimeNumberNeeded ;
int TestNumber = 0 ;
while (PrimeCounter < PrimeNumberNeeded)
{
TestNumber ++ ;
for (int Divisor=2;Divisor<TestNumber;Divisor++)
{
if (TestNumber%Divisor==0)
Isprime =false ;
break ;
}
if (Isprime ==true)
{
PrimeCounter++ ;
}
}
// end of while
if (PrimeCounter == PrimeNumberNeeded)
{
cout << "prime couter is: " << PrimeCounter<< "\nTest number is: "<< TestNumber<< endl;
}
system ("pause") ;
return 0;
please put [code.] before your code, and then [/code.] at the end of it (remove those "." when you do it for real). Also, indenting code makes it much easier to read.
If I enter a number higher than 3 it the program continues to loop without terminating. i think the problem is in the for loop but can not figure it out
.
sorry about the poor programming
#include <iostream>
#include <cstdlib>
#include <cstdio>
usingnamespace std;
int main()
{
int PrimeCounter = 0 ;
int PrimeNumberNeeded ;
bool Isprime ;
Isprime = true ;
cout << "Please enter the Prime Needed: " ;
cin >> PrimeNumberNeeded ;
int TestNumber = 0 ;
while (PrimeCounter < PrimeNumberNeeded)
{
TestNumber ++ ;
for (int Divisor=2;Divisor<TestNumber;Divisor++)
{
if (TestNumber%Divisor==0)
Isprime =false ;
break ;
}
if (Isprime ==true)
{
PrimeCounter++ ;
}
}
// end of while
if (PrimeCounter == PrimeNumberNeeded)
{
cout << "prime couter is: " << PrimeCounter<< "\nTest number is: "<< TestNumber<< endl;
}
system ("pause") ;
return 0;
}
Do you see any problems now? You never reset Isprime to true, so once it is false it's always false. The for loop is statement is missing brackets, so it will always iterate only once.
~Don't know why you have cstdlib and cstdio in there~
Thanks for the help, those includes where in a program i was given to start with and I never took them out.
Looking at the indentation makes it easier even if I did write it.
#include <iostream>
#include "conio.h"
usingnamespace std;
void main()
{
int X, Y, Z;
cout << "Please insert a number" << endl;
cin >> Z;
X=Z/2;
Y=X*2;
if(Z==Y)
cout << "The Number is prime" << endl;
else
cout << "The Number is not prime" << endl;
system("pause");
}
i dont understand what ur trying to do but this is an easy way to check if prime could even be just used as a function
HI All
I have the solution thank you. All help was greatly Appreciated, Like lowestone pointed out. I did not reset isprime back to true and the bracket in the for loop. Once this was fixed it worked.
1 is not a prime number i fixed that when I noticed that when the program spat out the first prime number was 1. I intialised the TestNumber to 1 that way when it entered the while loop it was incremented to 2.
I could make it simplier as suggested by using functions but I have not got that part of my books yet.
I find learning this a little slow as I find it difficult coming up with programs to write testing my knowledge.
PS
any good books out there to help teach this stuff
On the other hand, a big part of programming is implementation. There are more than one way to get primes, you know? I would recommend only using things you have learned in class (while loops/ifs, but not functions yet).
An early program I made was a digital clock using time.h
1 2 3 4 5 6 7 8
#include <time.h>
int main ()
{
time_t seconds;
seconds = time (NULL); // seconds now holds the amount of seconds since Jan 1, 1970 12:00 AM.
return 0;
}
At that time I couldn't use while/ifs so some interesting type casting went on. At first I wanted to get the date (month/day/year), but I still don't exactly know how to get a month without an if statement.
Thanks for the Tips, the test programs are an help
My job is a service Engineer. When I get software from our software department, When I came across bugs I used to say how hard can this be. Now I know and have never complained again. Its a thankless job