Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Last active November 5, 2019 08:54
Show Gist options
  • Save pinglunliao/2057acbcea9be13305de to your computer and use it in GitHub Desktop.
Save pinglunliao/2057acbcea9be13305de to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
using namespace std;
/*
15 28 6 56 60000 22 496 0
15 DEFICIENT
28 PERFECT
6 PERFECT
56 ABUNDANT
60000 ABUNDANT
22 DEFICIENT
496 PERFECT
*/
short checkPerfection(int n)
{
int sum = 0;
for( int i = 1; i <= n/2; i++ )
if( n % i == 0 )
sum += i;
if( n == sum )
return 0; // perfect
else if( n > sum )
return -1; // deficient
else if( n < sum )
return 1; // abundant
}
void prnMsg(short choice)
{
switch( choice )
{
case 0:
cout << "PERFECT";
break;
case 1:
cout << "ABUNDANT";
break;
case -1:
cout << "DEFICIENT";
break;
default:
break;
}
cout << endl;
}
int main()
{
int input[100];
int size = 0;
while( cin >> input[size] )
{
if( input[size] == 0 )
break;
size++;
}
cout << "PERFECTION OUTPUT" << endl;
for( int i = 0; i < size; i++ )
{
cout.width(5);
cout << input[i] << " ";
prnMsg(checkPerfection(input[i]));
}
cout << "END OF OUTPUT";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment