-
October 21st, 2012, 04:45 PM
#1
help me
I don't know what should I doing. About cmath. Here
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
int i;
int is_prime = true;
cout << "Enter a number and press Enter: ";
cin >> n;
i = 2;
while (i <= sqrt(n)) {
if (n % i == 0)
is_prime = false;
i++;
}
if (is_prime)
cout << "Number is prime." << endl;
else
cout << "Number is not prime." << endl;
system("pause");
return 0;
}
Please fix it for me. Thanks
-
October 21st, 2012, 05:02 PM
#2
Re: help me
I mean, Number 15 is error. It is said that C:\Users\Duy N. Le\Desktop\C++\prime1.cpp
call of overloaded `sqrt(int&)' is ambiguous
-
October 21st, 2012, 05:23 PM
#3
Re: help me
Check the documentation for sqrt and see what type of argument it expects. Compare that to what you're passing it.
-
October 24th, 2012, 08:52 PM
#4
Re: help me
Do you have to use the sqrt() function?
Here's a code I did a while ago, it lists prime numbers from a given range.
It can be modified to just tell if a number is prime.
Hope it Helps
Code:
// Prime Number Notes 10-7-2011.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include "resource.h"
using namespace std;
void DisplayEvens(int, int);
int main()
{
// Declare and init two intergers
// Beginning num and ending numbers
int intBeginNum = 0;
int intEndNum = 0;
// Prompt user for range
cout << "Enter a beginning number for the range:" << endl;
cin >> intBeginNum;
cout << "Enter an ending number for the range:" << endl;
cin >> intEndNum;
DisplayEvens(intBeginNum, intEndNum);
system ("pause");
return 0;
}
void DisplayEvens(int intBeginNumDE, int intEndNumDE)
{
// Initialization and declare
// this is done inside of for loop
bool bolPrime = false;
for (int i = intBeginNumDE; i <= intEndNumDE; i++)
{
for (int j = 2; j < 10; j++)
{
if (i != j && i != 1)
{
if (i % j != 0)
bolPrime = true;
else
{
bolPrime = false;
break;
}
}
}
if (bolPrime)
cout << "Prime Number: " << i << endl;
}
return;
}
-
October 24th, 2012, 09:12 PM
#5
Re: help me
Never mine. I already problem solved because I found out it. Show it here.
Code:
(i <= sqrt((double)n))
It would worked. :-)
-
October 24th, 2012, 09:35 PM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|