#include “stdafx.h”

#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n; // Number to test for prime-ness
int i; // Loop counter
int is_prime = true; // Boolean flag…
// Assume true for now.
// Get a number from the keyboard.

cout << “Enter a number and press ENTER: ”;
cin >> n;

// Test for prime by checking for divisibility
// by all whole numbers from 2 to sqrt(n).
i = 2;

while (i <= sqrt(n)) { // While i is <= sqrt(n),
if (n % i == 0) // If i divides n,
is_prime = false; // n is not prime.
i++; // Add 1 to i.
}
// Print results
if (is_prime)
cout << “Number is prime.” << endl;
else
cout << “Number is not prime.” << endl;
system(“PAUSE”);
return 0;
}


Þetta á semsagt að tjekka hvort tala sjé prime tala
En ég fæ alltaf villu
(22): error C2668: ‘sqrt’ : ambiguous call to overloaded function
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(589): could be ‘long double sqrt(long double)’
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(541): or ‘float sqrt(float)’
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(127): or ‘double sqrt(double)’
1> while trying to match the argument list ‘(int)’
01001101 01101010 01100001 01110111 00100000 00111111