Post: need help to fix my C++ program please.
02-03-2012, 09:14 PM #1
snig08
Keeper
(adsbygoogle = window.adsbygoogle || []).push({}); ok i want to make a program using c++ that will tell you if a number is prime.

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
double i, n;
bool is_prime = true;

cout << "Enter a number to test for primeness\n";
cin >> n;

for (double i = 2; i <= sqrt (n); i++)
if (n % i == 0)
is_prime = false;

if (is_prime)
cout << "This number is prime\n";

else
cout << "This number is not prime\n";
system("PAUSE");
return 0;




my problem is that I can use double but when I try to use remainder (%) I cant it says that my two values need to be an integer. but when I use integers my sqrt wont work because it has to be a double. can any one help and tell me where I have gone wrong.
02-04-2012, 06:14 AM #2
Epic?
Awe-Inspiring
Originally posted by snig08 View Post
ok i want to make a program using c++ that will tell you if a number is prime.

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
double i, n;
bool is_prime = true;

cout << "Enter a number to test for primeness\n";
cin >> n;

for (double i = 2; i <= sqrt (n); i++)
if (n % i == 0)
is_prime = false;

if (is_prime)
cout << "This number is prime\n";

else
cout << "This number is not prime\n";
system("PAUSE");
return 0;




my problem is that I can use double but when I try to use remainder (%) I cant it says that my two values need to be an integer. but when I use integers my sqrt wont work because it has to be a double. can any one help and tell me where I have gone wrong.


Fixed your code:

    
#include <iostream>
#include <cmath>

int main() {
double numberToTest;

bool isPrime = true;

std::cout << "Enter a number to test for primality" << std::endl;
std::cin >> numberToTest;

for (int i = 2; i <= sqrt(numberToTest); i++)
if (fmod(numberToTest, i) == 0)
isPrime = false;

if (isPrime)
std::cout << "The number is prime" << std::endl;
else
std::cout << "The number is not prime" << std::endl;

return 0;
}


Use fmod instead of the modulus operator.

By the way, sqrt should work just fine on integer values.

Oh, and your program doesn't actually work. It tells me all numbers less than or equal to one are prime, which is wrong.
02-04-2012, 04:14 PM #3
snig08
Keeper
thanks for that m8 its appreciated. i have updated it now and i have fixed that number 1 issue. i just added another if, it does work but i have to admit it looks ugly and there has to be some way to combine it.


#include<iostream>
#include<cmath>

int main()
{
double n;
bool is_prime = true;

std::cout << "Enter a number to test for primality\n";
std::cin >> n;
for (int i = 2;i <= sqrt (n); i++)
if (fmod (n, i) == 0)
is_prime = false; //is there a way to add the 'if' statement to the 'for' statement.
if (n==1)
is_prime = false;

if (is_prime)
std::cout << "This number is prime\n";

else
std::cout << "This number is not prime\n";
system("PAUSE");
return 0;
}
02-04-2012, 07:02 PM #4
Epic?
Awe-Inspiring
Originally posted by snig08 View Post
thanks for that m8 its appreciated. i have updated it now and i have fixed that number 1 issue. i just added another if, it does work but i have to admit it looks ugly and there has to be some way to combine it.


#include<iostream>
#include<cmath>

int main()
{
double n;
bool is_prime = true;

std::cout << "Enter a number to test for primality\n";
std::cin >> n;
for (int i = 2;i <= sqrt (n); i++)
if (fmod (n, i) == 0)
is_prime = false; //is there a way to add the 'if' statement to the 'for' statement.
if (n==1)
is_prime = false;

if (is_prime)
std::cout << "This number is prime\n";

else
std::cout << "This number is not prime\n";
system("PAUSE");
return 0;
}


In the future, please put your code in CODE BBCode tags, like so: [CODE] Code goes here [/CODE].

Anyways, you might want to learn more about You must login or register to view this content. if you're serious about designing a working, efficient algorithm.

What you're doing is attempting a naive approach. Now, there aren't really any problems with that (unless you're trying to squelch out every bit of efficiency).

Anyways, here's a naive primality test that I wrote in Python, it's fairly similar to pseudo-code, so you may find it of use:
    
import math

def isPrime(n):
if n <= 1: return False
elif n == 2: return True
elif n % 2 == 0: return False
else:
prime = True
divisor = 3
upperLimit = (math.sqrt(n) + 1)

while divisor <= upperLimit:
if n % divisor == 0:
prime = False

divisor += 2

return prime



Also, a quick style note about the code you've been posting. Aside from posting future code in CODE tags, when doing console output with C++, you should try to use endl instead of "\n" when ending a line (in most cases). Also, don't forget to use plenty of comments in your program.
Last edited by Epic? ; 02-04-2012 at 07:21 PM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo