| | |
prime numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 24
Reputation:
Solved Threads: 0
i know how to find prime numbers of several numbers, does anyone know hoew to find a prime sumber of a single number inputed by the ser using the following function and printing out if the number is prime
int mai()//asks for input
{
int num;
cout << "enter number:";
cin >> num;
call IsPrime;
cout if prime or not
}
bool IsPrime(int n)
// precondition: n >= 0
// postcondition: returns true if n is prime, else returns false
// returns false if precondition is violated
{
if (n < 2) // 1 and 0 aren't prime
{ return false; // treat negative #'s as not prime
}
else if (2 == n) // 2 is only even prime number
{ return true;
}
else if (n % 2 == 0) // even, can't be prime
{ return false;
}
else // number is odd
{ int limit = int(sqrt(n) + 1); // largest divisor to check
int divisor = 3; // initialize to smallest divisor
// invariant: n has no divisors in range [2..divisor)
while (divisor <= limit)
{ if (n % divisor == 0) // n is divisible, not prime
{ return false;
}
divisor += 2; // check next odd number
}
return true; // number must be prime
}
}
any suggestions would greatly be appreciated
int mai()//asks for input
{
int num;
cout << "enter number:";
cin >> num;
call IsPrime;
cout if prime or not
}
bool IsPrime(int n)
// precondition: n >= 0
// postcondition: returns true if n is prime, else returns false
// returns false if precondition is violated
{
if (n < 2) // 1 and 0 aren't prime
{ return false; // treat negative #'s as not prime
}
else if (2 == n) // 2 is only even prime number
{ return true;
}
else if (n % 2 == 0) // even, can't be prime
{ return false;
}
else // number is odd
{ int limit = int(sqrt(n) + 1); // largest divisor to check
int divisor = 3; // initialize to smallest divisor
// invariant: n has no divisors in range [2..divisor)
while (divisor <= limit)
{ if (n % divisor == 0) // n is divisible, not prime
{ return false;
}
divisor += 2; // check next odd number
}
return true; // number must be prime
}
}
any suggestions would greatly be appreciated
Greetings cblue,
> does anyone know hoew to find a prime sumber of a single number inputed by the ser using the following function and printing out if the number is prime
Yes, I do know, its quite possible too. How did this work? Simple, the if statement is used to express decisions. Formally the syntax is: Where the else is optional. The expression is evaluated; if it is true (if has a non-zero value), statement1, is executed. If it is false, (if has value of zero) and if there is an else part, statement2 is executed instead.
Since an if simply tests the numeric value of an expression, certain coding shortcuts are possible: Instead of: If you have further questions, please feel free to ask. Hope this helps.
- Stack Overflow
> does anyone know hoew to find a prime sumber of a single number inputed by the ser using the following function and printing out if the number is prime
Yes, I do know, its quite possible too.
int main() { int num; cout << "enter number:"; cin >> num; if (isPrime(num)) cout << "Number is prime" << endl; else cout << "Number is not prime" << endl; return 0; } // Add isPrime code here
if (expression) statement1 else statement2
Since an if simply tests the numeric value of an expression, certain coding shortcuts are possible:
if (expression)if (expression != 0)- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.
IRC
Channel: irc.daniweb.com
Room: #c, #shell
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Similar Threads
- (Noob) need some help w/ Prime Numbers (C++)
- Prime Numbers (C)
- prime numbers homework trouble (C++)
- help with array that determines prime numbers (C)
- prime numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: Create Sound
- Next Thread: Dealing With XML in Visual C++
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





