问题
the problem is : "Write a function to find out if a number is a prime or perfect number."
so far i have worked on the perfect part first and this is what i have:
#include <iostream>
using namespace std;
bool perfectNumber(int);
int main()
{
int number;
cout<<"Please enter number:\n";
cin>>number;
bool perfectNumber(number);
return 0;
}
bool perfectNumber(int number)
{
int i;
int sum=0;
for(i=1;i<=number/2;i++)
{
if(number%i==0)
{
sum+=i;
}
}
if (sum==number)
return i;
else
return 0;
}
HOWEVER, there seems to be errors on this code. I have looked over the book but nothing talks about this topic. i would like to get advice on how to fix this code.
thanks!
回答1:
bool perfectNumber(number);
This does not call the perfectNumber
function; it declares a local variable named perfectNumber
of type bool
and initializes it with the value of number
converted to type bool
.
In order to call the perfectNumber
function, you need to use something along the lines of:
bool result = perfectNumber(number);
or:
bool result(perfectNumber(number));
On another note: if you are going to read input from a stream (e.g. cin>>number
), you must check to be sure that the extraction of the value from the stream succeeded. As it is now, if you typed in asdf
, the extraction would fail and number
would be left uninitialized. The best way to check whether an extraction succeeds is simply to test the state of the stream:
if (cin >> number) {
bool result = perfectNumber(number);
}
else {
// input operation failed; handle the error as appropriate
}
You can learn more about how the stream error states are set and reset in Semantics of flags on basic_ios. You should also consult a good, introductory-level C++ book for more stream-use best practices.
回答2:
void primenum(long double x) {
bool prime = true;
int number2;
number2 = (int) floor(sqrt(x));// Calculates the square-root of 'x'
for (int i = 1; i <= x; i++) {
for (int j = 2; j <= number2; j++) {
if (i != j && i % j == 0) {
prime = false;
break;
}
}
if (prime) {
cout << " " << i << " ";
c += 1;
}
prime = true;
}
}
回答3:
bool isPerfect( int number){
int i;
int sum=0;
for(i=1;i<number ;i++){
if(number %i == 0){
cout<<" " << i ;
sum+=i;
}
}
if (sum == number){
cout<<"\n \t\t THIS NUMBER >>> "<< number <<" IS PERFECT \n\n";
return i;
}else if (sum |= number) {
cout<<"\nThis number >>> " << number <<" IS NOT PERFECT \n\n";
return 0;
}
}
回答4:
#pragma hdrstop
#include <tchar.h>
#include <stdio.h>
#include <conio.h>
//---------------------------------------------------------------------------
bool is_prim(int nr)
{
for (int i = 2; i < nr-1; i++) {
if (nr%i==0) return false;
}
return true;
}
bool is_ptr(int nr)
{
int sum=0;
for (int i = 1; i < nr; i++) {
if (nr%i==0) {
sum=sum+i;
}
}
if (sum==nr) { return true;
}
else return false;
}
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
int numar;
printf ("Number=");scanf("%d",&numar);
if (is_prim(numar)==true) { printf("The number is prime");
}
else printf("The number is not prime");
if (is_ptr(numar)==true) { printf(" The number is perfect");
}
else printf(" The number is not perfect");
getch();
return 0;
}
来源:https://stackoverflow.com/questions/4424002/deciding-if-a-number-is-perfect-or-prime