Hello,
im pretty new with c++ and i can only get formating correct but not the if else stuff on where to show an X.
complete a C++ program that will
read a series of integers from a file (via Linux redirection) and display (to the screen) a
table with 4 columns (n, abundant, deficient, perfect)
• for each integer read from the file, display
the integer
an 'X' or a blank in the appropriate column of the table (note: if the integer does
not fall into any of the 3 categories, leave all columns blank)
this is the math that it has to due for each integer.
Given an integer, n, the integers a and b are said to be factors (or divisors) of n if a*b = n.
Given an integer, n, that is greater than 0,
• n is an abundant number if the sum of its factors is greater than 2* n
• n is a deficient number if the sum of its factors is less than 2* n
• n is a perfect number if the sum of its factors is equal to 2* n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int main ()
{
int data, i = 1, sum = 0;
cin >> data;
cout << setw(12) << "Input #" << setw(12) << "Abundant" << setw(12) << "Deficient" <<setw(12) << "Perfect" << endl;
while (data)
{
while (i <= data)
{
if (data % i == 0)
sum = sum + i;
i++;
}
char a = 'x';
char d= 'x';
char p= 'x';
if (sum == data * 2)
{
p = 'X';
}
else if (sum == 0)
{
a= ' ';
d= ' ';
p= ' ';
}
else if (sum > data * 2)
{
a = 'X';
}
else if (sum < data * 2)
{
d = 'X';
}
cout << setw(12) << data << setw(12) << a << setw(12) << d <<setw(12) << p << endl;
cin >> data;
}
return 0;
}
|