The purpose of this assignment is to give practice programming in writing functions, developing algorithms and using
loop structures.
Positive integers can have the following personalities: prime or composite, happy or unhappy, square or not
square, smug or not smug, and honest or dishonest.
You will write a C++ program which will contain the following functions : isPrime, isHappy, isSquare, isSmug,
isHonest. All of the functions return boolean (i.e. true or false).
Note that, in numberPersonalities.cpp, all of methods currently return false. This done so that your program
can compile while you work on one method at time.
Personality Description
1. Prime and composite numbers
A positive number is prime if its only positive divisors are itself and one. For example, 7 is prime because it is
evenly divisible by 1 and 7, but not by any number in between (in particular, it is not evenly divisible by 2, 3, 4, 5,
or 6).
A positive number is composite if it is not prime. For example, 10 is composite because it is evenly divisible by 2
and 5; 12 is composite because it is evenly divisible by 2, 3, 4, and 6. As a special case, 1 is considered to be
composite.
2. Happy and unhappy numbers
Repeatedly apply the following procedure to a number:
1. Square each of the digits of the number.
2. Add the squares together.
If by doing this you eventually get to 1, then the number is happy.
For example, if you start with 13:
• 1*1 + 3*3 = 1 + 9 = 10
• 1*1 + 0*0 = 1 and the number is happy.
If instead you get into an infinite loop, the number is unhappy. So if your program runs forever, the number is
unhappy. This isn't a very useful test, however. Fortunately, every unhappy number will eventually get into this
cycle:
4, 16, 37, 58, 89, 145, 42, 20, 4, ...
so if you get any one of these numbers, then you can stop and conclude that the number is unhappy. Doing
this in your method is fairly simple because all of the above numbers in the cycle will give you 4. For this
reason, to check if a number is unhappy, just compare the sums of squares of its digits with 4. If you get 4,
then number is unhappy.
Programming note : You can get the last digit of a number n with the expression (n % 10). You can subsequently
discard this digit by doing an "integer division" by 10; when you get a zero, there are no more digits.
3. Square numbers
A square number is a number of the form 1 + 3 + 5 + 7 + ... + n, for some odd positive n. (The
figure should help you understand why this definition is the same as the usual definition of square
numbers.)
4. Smug numbers
A number is smug if it is the sum of two square numbers. The first few smug numbers are 2(1+1), 5 (1+4), 8
(4+4), 10 (1+9), 13 (4+9), etc.
5. Honest and dishonest numbers
A number is dishonest if it "pretends" to be a square number, but isn't one. Specifically, n is dishonest if there
is a number k such that n/k = k (using integer division), but k*k is not n. A number is honest if it is not dishonest.
Final Output
For each of the numbers 1 through 100, you will test the methods (functions) you developed and according to
the method return values, print out the numbers, one per line, along with a list of its characteristics (on the
same line).
Your output should look approximately like this:
1 composite, happy, square, not smug, honest
2 prime, unhappy, not square, smug, honest
3 prime, unhappy, not square, not smug, honest
4 composite, unhappy, square, not smug, honest
5 prime, unhappy, not square, smug, dishonest
...
Hence you need to also write a main method that will declare a variable as int limit = 100; and test the numbers 1 through
limit, inclusive. This makes it easy to change the limit later. Also test your program with two or three large numbers (say,
five or six digits), to make sure that your program can handle them.
Program skeleton
Your program must follow the structure given in the NumberPersonalities.cpp (that is, you need to fill in the blanks
ONLY ..
/*-------------------------------------------------
* CSE1101 Introduction to Programming Homework 2
* Author:
* Date:
*
*--------------------------------------------------*/
#include <iostream>
using namespace std;
//This method checks if the number (var) is a prime or composite.
//If number is prime then it returns true (i.e.prime), else false (i.e. composite)
bool isPrime(int var) {
//Fill in the code
return false;
}//end of isPrime
//This method checks if the number (var) is a happy.
//If number is happy then it returns true, else false
bool isHappy(int var) {
//Fill in the code
return false;
}//end of isHappy
//This method checks if the number (var) is square.
//If number is square then it returns true, else false
bool isSquare(int var) {
//Fill in the code
return false;
}//end of isSquare
//This method checks if the number (var) is smug.
//If number is smug then it returns true, else false.
bool isSmug(int var) {
//Fill in the code
return false;
}//end of isSmug
//This method checks if the number (var) is honest.
//If number is honest then it returns true, else false.
bool isHonest(int var) {
//Fill in the code
return false;
}//end of isHonest
int main() {
/*
* Final Output - Sample
*
1 composite, happy, square, not smug, honest
2 prime, unhappy, not square, smug, honest
3 prime, unhappy, not square, not smug, honest
4 composite, unhappy, square, not smug, honest
5 prime, unhappy, not square, smug, dishonest
...
*
*/
//Fill in code
}