2

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!

false
  • 10,264
  • 13
  • 101
  • 209
carla
  • 169
  • 1
  • 2
  • 7

4 Answers4

7
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.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
1
 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;
     }
 }
IByBaz
  • 11
  • 1
1
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;
    }
}
m.qayyum
  • 409
  • 2
  • 9
  • 24
  • 1
    bool prime = true; // Calculates the square-root of 'x' Really? it's also important to place comments correctly. – Eric Fortis Dec 12 '10 at 21:18
  • 1
    This is so wrong. If you are testing if x is prime, get rid of the outer loop. If you are testing for all primes in [1,x] then the bound on the inner loop should be sqrt(i) not sqrt(x). And in that case this is a ridiculously inefficient way to test for primes in that range. See http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes for a much better method. – Chris Hopman Dec 12 '10 at 21:24
-1
#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;
}
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
opc0de
  • 11,557
  • 14
  • 94
  • 187
  • is_ptr returns false or true for a perfect number and is_prim returns true or false for a prime number – opc0de Dec 12 '10 at 21:22
  • 4
    *Please* format your code. Didn't you look at the preview and notice "this is unreadable" before posting the answer? In the editor, select the source code, then click the `101010` button. – jalf Dec 12 '10 at 22:14
  • @jalf `101010` means `fine` ? I didn't know :-) – Luc M Jan 09 '12 at 20:06
  • 1
    funny. ;) btw, to avoid confusion if others read this, the `{ }` button *used* to show something like `10010` instead. – jalf Jan 10 '12 at 09:48