Deciding whether a number is perfect or prime

The problem is this: "Write a function to find out if a number is prime or perfect."

so far I have been working on the ideal part of the 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 are errors in this code. I looked through the book, but nothing is said about this topic. I would like some advice on how to fix this code.

thanks!

+2
c ++ numbers primes perfect-numbers modulus
source share
4 answers
 bool perfectNumber(number); 

This does not call the perfectNumber function; it declares a local variable named perfectNumber type bool and initializes it with the value number converted to type bool .

To call the perfectNumber function, you need to use something line by line:

 bool result = perfectNumber(number); 

or

 bool result(perfectNumber(number)); 

In another note: if you are going to read input from a stream (for example, cin>>number ), you must make sure that retrieving the value from the stream succeeds. As now, if you typed asdf , the extraction will fail and number will remain uninitialized. The best way to verify the success of the extraction is to simply check the status 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 error conditions of the stream are set, and reset to Flags semantics on basic_ios . You should also consult a good, introductory version of the C ++ book for more efficient flow control methods.

+7
source share
 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; } } 
+1
source share
  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; } } 
+1
source share
 #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; } 
-one
source share

Source: https://habr.com/ru/post/650073/


All Articles