Not quite sure where my problem lies... I am still relatively new to programming. The code is supposed to determine if a number is prime or not. The problem is that, say for instance, the user enters 15. 15 is not prime but it will say that it is... the problem is the factor of 3 with another odd number... but I don't really see where to fix it... :|
As far as i can tell it works with everything except for the factor of 3 paired with another odd number... besides that it calls prime or not correctly...
any help is appreciated, a hint in the right direction would be better! Thanks for your time...
/*
Write a program that prompts the user to input a positive integer. It should then
output a message indicating whether the number is a prime number. (Note: an
even number is prime if it is 2. An odd integer is prime if it is not divisible by
any odd integer less than or equal to the square root of the number.)
*/
#include <iostream>
#include <cmath>
#include <cstdlib>
usingnamespace std;
int main()
{
double doubleNumber, doubleSquareRootOfNumber;
int container = 0, counter = 1, number, squareRootOfNumber;
bool flag = false, prime = false;
cout << "Please enter a positive integer: ";
cin >> doubleNumber;
cout << endl;
doubleSquareRootOfNumber = sqrt(doubleNumber);
number = (int)doubleNumber;
squareRootOfNumber = (int)doubleSquareRootOfNumber;
while(flag != true)
{
if (number == 1 || number == 2)
{
cout << "The number " << number << " is a prime number..." << endl << endl;
flag = true;
break;
}
if (number > 2)
{
while(counter <= squareRootOfNumber)
{
counter++;
container = number % counter;
if(container == 0)
{
prime = false;
flag = true;
break;
}
else
{
prime = true;
flag = true;
break;
}
}
if(prime == true)
{
cout << "The number " << number << " is a prime number..." << endl << endl;
}
if(prime == false)
{
cout << "The number " << number << " is not a prime number..." << endl << endl;
}
}
}
system("PAUSE");
return 0;
}
if(container == 0)
{
prime = false;
flag = true;
break;
}
else
{
prime = true;
flag = true;
break;
}
The "else" ensures that the parent while loop always breaks at the first iteration, not just while counter <= squareRootOfNumber evaluates to true. Is this intentional?
no, it wasn't... could that be why it won't work on factors of 3? I noticed that the sqrt of most small numbers dont make it past 4 so the counter does not get high enough to accurately determine the prime or not nature of the number...
that actually looks pretty good! Sorry for late reply.... had to go to bed.... and now I have to go to work.... but thank you for your help .... I'm checking this out when I get off :-)
// prime.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int num;
char ch;
do
{
int prime=0;
cout<<"Enter a number ";
cin>>num;
for(int i=1;i<=num/2;i++)
{
if(num%i==0)
{
prime++;
}
else
{
}
}
if(prime>1)
cout<<"Number is not prime "<<num<<endl;
else
cout<<"Number is prime "<<num<<endl;
cout<<"Do you want to do it again ";
cin>>ch;
}
while(ch=='y' || ch=='Y');
return 0;
}