#include<iostream> #include <iomanip> using namespace std; const int COLWIDTH =
ID: 3609890 • Letter: #
Question
#include<iostream>
#include <iomanip>
using namespace std;
const int COLWIDTH = 11; //width of columns in factortable
bool isfibonacci(int);
int sumdigits(int);
void findfactors(int,int&);
int main()
{
int num; //input value to beanalyzed
int digitsum; //sum of the digits in num
int factcount; //number of factors of num if num>0
cout << "**************Hubbard NumberAnalyzer*************" << endl;
cin >> num;
while (!cin.eof())
{
cout << num << " is";
if (!isfibonacci(num))
cout << "NOT";
cout << "fibonacci." <<endl;
digitsum = sumdigits(num);
cout << "The sum of the digitsin " << num << " = "
<< digitsum << "." << endl;
if (num > 0)
{
findfactors(num,factcount);
if(factcount==2)
cout<<num<<" is prime."<<endl;
else
cout<<num<<" is NOT prime."<<endl;
cout<< endl;
}
cout <<"------------------------------------" << endl;
cin >> num;
}
return 0;
}
int sumdigits(int n)
//Given an integer, n, return the sum of the digits in n.
//If n < 0, disregard the negative sign. For example, if
//n is -345, return 12.
{
int digit; //variable used to hold the right most number
int digitsum=0;
while (n>0)
{
digit=n%10;
n/=10;
digitsum+=digit;
}
return digitsum;
}
if (line%5==0)
cout<<endl;
}
cout<<endl;
Explanation / Answer
#include<iostream>
#include <iomanip>
using namespace std;
const int COLWIDTH = 11; //width of columns in factortable
bool isfibonacci(int);
int sumdigits(int);
void findfactors(int,int&);
int main()
{
int num; //input value to beanalyzed
int digitsum; //sum of the digits in num
int factcount; //number of factors of num if num>0
cout << "**************Hubbard NumberAnalyzer*************" << endl;
cin >> num;
while (!cin.eof())
{
cout << num << " is";
if (!isfibonacci(num))
cout << "NOT";
cout << "fibonacci." <<endl;
digitsum = sumdigits(num);
cout << "The sum of the digitsin " << num << " = "
<< digitsum << "." << endl;
if (num > 0)
{
findfactors(num,factcount);
if(factcount==2)
cout<<num<<" is prime."<<endl;
else
cout<<num<<" is NOT prime."<<endl;
cout<< endl;
}
cout <<"------------------------------------" << endl;
cin >> num;
}
return 0;
}
bool isfibonacci(int anum)
//Given an integer, anum, return true if anum is part of the
//Fibonacci series, return false if it is not.
{
int first; // first number in Fibonacci series
int second; //second number in Fibonacci series
int next=0; //next number in series to determine if anum
//is part of Fibonacci series
first=0;
second=1;
while(anum>next)
{
next=first+second;
if(anum==next)
return true;
first=second;
second=next;
}
return false;
}
int sumdigits(int n)
//Given an integer, n, return the sum of the digits in n.
//If n < 0, disregard the negative sign. For example, if
//n is -345, return 12.
{
int digit; //variable used to hold the right most number
int digitsum=0;
while (n>0)
{
digit=n%10;
n/=10;
digitsum+=digit;
}
return digitsum;
}
void findfactors(int num,int& factors)
//Findfactors is passed 2 parameters, num, an integer > 0,and
//factors, an integer used to count num's factors. The function
//should display the factors for num in a table with 5 columns.
//It should also display the final factor count which will be
//passed back to main.
{
int i; //loop counter
int line; //row counter
factors=0;
cout<<"The factors of "<<num<<"are"<<endl;
for (i=1; i<=num; i++)
if (num%i==0)
{
factors++;
cout<<setw(6)<<right<<i;
line++;
if (line%5==0)
cout<<endl;
}
cout<<endl;
cout<<num<<" has "<<factors<<"factors"<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.