CSCI 240 | Spring 2025 |
For this assignment, write some functions that can be used to manipulate and test an integer number.
The main routine has been provided (it can be downloaded from Blackboard or from http://faculty.cs.niu.edu/~byrnes/csci240/pgms/assign6.cpp) or the autograder. All that is needed for this assignment is to add the protoypes and implementations for the various required functions to the CPP file that is provided.
The following functions are required for this assignment:
This function will calculate and return a sum by adding the individual digits in an integer number. For example, if num contains 641, the function should return 11 (the sum of 6 + 4 + 1).
The function takes one integer argument: the number to use in calculating the sum. It returns an integer value: the sum of the digits.
This function will reverse and return the digits in an integer number. For example, if num contains 641, the function should return 146.
The function takes one integer argument: the number to be reversed. It returns an integer value: the reversed number.
Remember from the daily that displayed the digits of a reversed number that modulus division by 10 and integer division by 10 will need to be used. For this function, rather than displaying the digits, an integer value with the reversed number will be created. This means that any leading 0s on the reverse number will not be part of the result.
When building the reversed number, keep in mind that multiplication by 10 can be used to "open a spot" to add a value to the reversed number. For example, using the original value of 641:
Iteration #1:
Iteration #2:
Iteration #3:
Now the digits in num have been reversed and the result is 146.
This function will test to see if an integer number is a palindrome. For an integer to be a palindrome, it must "read" the same forward and backward. So 464 is a palindrome, while 15 is not a palindrome.
The function takes one integer argument: the number to test. It returns a boolean value that represents whether the integer argument is (true) or is not (false) a palindrome.
To determine if an integer number is a palindrome, take advantage of the reverse() function that was previously written.
This function will test to see if an integer number is a prime number. For an integer to be a prime number, it must be divisible by only 1 and itself. So 11 is a prime number, while 15 is not a prime number.
The function takes one integer argument: the number to test. It returns a boolean value that represents whether the integer argument is (true) or is not (false) a prime number.
There are a number of different ways to determine if an integer number is a prime number, the following is just one of the ways. Write a loop that is controlled by an integer counter. The counter should start with a value of 2 and the loop should continue one value at a time as long as the counter is less than or equal to half of the integer number that is being tested. Inside of the loop, if the integer number is divisible by the counter, the integer number is not prime and false should be returned.
If the loop is able to complete execution, the integer number is prime and true should be returned.
As with the previous assignments and the assignments until the end of the semester, complete program documentation is required. For this assignment, that means that line documentation (in main and the functions) AND function documentation boxes are needed. In regards to line documentation, there is no need to document every single line, but logical "chunks" of code should be preceded by a line or two that describes what the "chunk" of code does.
Each function must have a documentation box explaining:
/*************************************************************** * A brief description of what the function does * * @param arg_name_1 brief description of the first argument * @param arg_name_2 brief description of the second argument * @param arg_name_3 brief description of the third argument * * @return a brief description of the possible return value(s) * * @note an optional note about the function that may be of * interest to someone using it ***************************************************************/
For example, the isPrime function might start with:
/*************************************************************** * This function determines if a number is a prime number * * @param num the number to be tested * * @return a boolean value that indicates if the number is or * is not prime ***************************************************************/
See the documentation standards on the course webpage for more examples or if further clarification is needed.
The main routine that has been provided may not be altered in the source code that is graded. However, as the program is being developed, you may comment out some of the function calls so that the functions can be coded one at a time. You may also want to think about making the loop execute fewer times as you're developing the functions.
Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.
What is the seed value? 1 The number is 41 ---------------------------------------- Adding the digits result 5 Reversing the digits result 14 Is the number a palindrome? No Is the number prime? Yes The number is 18467 ---------------------------------------- Adding the digits result 26 Reversing the digits result 76481 Is the number a palindrome? No Is the number prime? No The number is 6334 ---------------------------------------- Adding the digits result 16 Reversing the digits result 4336 Is the number a palindrome? No Is the number prime? No The number is 26500 ---------------------------------------- Adding the digits result 13 Reversing the digits result 562 Is the number a palindrome? No Is the number prime? No The number is 19169 ---------------------------------------- Adding the digits result 26 Reversing the digits result 96191 Is the number a palindrome? No Is the number prime? No The number is 15724 ---------------------------------------- Adding the digits result 19 Reversing the digits result 42751 Is the number a palindrome? No Is the number prime? No The number is 11478 ---------------------------------------- Adding the digits result 21 Reversing the digits result 87411 Is the number a palindrome? No Is the number prime? No The number is 29358 ---------------------------------------- Adding the digits result 27 Reversing the digits result 85392 Is the number a palindrome? No Is the number prime? No The number is 26962 ---------------------------------------- Adding the digits result 25 Reversing the digits result 26962 Is the number a palindrome? Yes Is the number prime? No The number is 24464 ---------------------------------------- Adding the digits result 20 Reversing the digits result 46442 Is the number a palindrome? No Is the number prime? No Test 5 numbers to see if they're palindromes... 1. What is the number? 123456 123456 is not a palindrome. 2. What is the number? 1234321 1234321 is a palindrome. 3. What is the number? 77 77 is a palindrome. 4. What is the number? 4343 4343 is not a palindrome. 5. What is the number? 97579 97579 is a palindrome.
What is the seed value? 7 The number is 61 ---------------------------------------- Adding the digits result 7 Reversing the digits result 16 Is the number a palindrome? No Is the number prime? Yes The number is 17422 ---------------------------------------- Adding the digits result 16 Reversing the digits result 22471 Is the number a palindrome? No Is the number prime? No The number is 15215 ---------------------------------------- Adding the digits result 14 Reversing the digits result 51251 Is the number a palindrome? No Is the number prime? No The number is 7040 ---------------------------------------- Adding the digits result 11 Reversing the digits result 407 Is the number a palindrome? No Is the number prime? No The number is 15521 ---------------------------------------- Adding the digits result 14 Reversing the digits result 12551 Is the number a palindrome? No Is the number prime? No The number is 6516 ---------------------------------------- Adding the digits result 18 Reversing the digits result 6156 Is the number a palindrome? No Is the number prime? No The number is 30152 ---------------------------------------- Adding the digits result 11 Reversing the digits result 25103 Is the number a palindrome? No Is the number prime? No The number is 11794 ---------------------------------------- Adding the digits result 22 Reversing the digits result 49711 Is the number a palindrome? No Is the number prime? No The number is 27727 ---------------------------------------- Adding the digits result 25 Reversing the digits result 72772 Is the number a palindrome? No Is the number prime? No The number is 20344 ---------------------------------------- Adding the digits result 13 Reversing the digits result 44302 Is the number a palindrome? No Is the number prime? No Test 5 numbers to see if they're palindromes... 1. What is the number? 945 945 is not a palindrome. 2. What is the number? 6556 6556 is a palindrome. 3. What is the number? 32574 32574 is not a palindrome. 4. What is the number? 11111 11111 is a palindrome. 5. What is the number? 58685 58685 is a palindrome.
What is the seed value? 1 The number is 16807 ---------------------------------------- Adding the digits result 22 Reversing the digits result 70861 Is the number a palindrome? No Is the number prime? No The number is 72425 ---------------------------------------- Adding the digits result 20 Reversing the digits result 52427 Is the number a palindrome? No Is the number prime? No The number is 33847 ---------------------------------------- Adding the digits result 25 Reversing the digits result 74833 Is the number a palindrome? No Is the number prime? No The number is 33809 ---------------------------------------- Adding the digits result 23 Reversing the digits result 90833 Is the number a palindrome? No Is the number prime? Yes The number is 97490 ---------------------------------------- Adding the digits result 29 Reversing the digits result 9479 Is the number a palindrome? No Is the number prime? No The number is 6570 ---------------------------------------- Adding the digits result 18 Reversing the digits result 756 Is the number a palindrome? No Is the number prime? No The number is 26534 ---------------------------------------- Adding the digits result 20 Reversing the digits result 43562 Is the number a palindrome? No Is the number prime? No The number is 36300 ---------------------------------------- Adding the digits result 12 Reversing the digits result 363 Is the number a palindrome? No Is the number prime? No The number is 63336 ---------------------------------------- Adding the digits result 21 Reversing the digits result 63336 Is the number a palindrome? Yes Is the number prime? No The number is 17637 ---------------------------------------- Adding the digits result 24 Reversing the digits result 73671 Is the number a palindrome? No Is the number prime? No Test 5 numbers to see if they're palindromes... 1. What is the number? 123456 123456 is not a palindrome. 2. What is the number? 1234321 1234321 is a palindrome. 3. What is the number? 77 77 is a palindrome. 4. What is the number? 4343 4343 is not a palindrome. 5. What is the number? 97579 97579 is a palindrome.
What is the seed value? 7 The number is 17648 ---------------------------------------- Adding the digits result 26 Reversing the digits result 84671 Is the number a palindrome? No Is the number prime? No The number is 6970 ---------------------------------------- Adding the digits result 22 Reversing the digits result 796 Is the number a palindrome? No Is the number prime? No The number is 26065 ---------------------------------------- Adding the digits result 19 Reversing the digits result 56062 Is the number a palindrome? No Is the number prime? No The number is 50144 ---------------------------------------- Adding the digits result 14 Reversing the digits result 44105 Is the number a palindrome? No Is the number prime? No The number is 95907 ---------------------------------------- Adding the digits result 30 Reversing the digits result 70959 Is the number a palindrome? No Is the number prime? No The number is 83818 ---------------------------------------- Adding the digits result 28 Reversing the digits result 81838 Is the number a palindrome? No Is the number prime? No The number is 85737 ---------------------------------------- Adding the digits result 30 Reversing the digits result 73758 Is the number a palindrome? No Is the number prime? No The number is 5408 ---------------------------------------- Adding the digits result 17 Reversing the digits result 8045 Is the number a palindrome? No Is the number prime? No The number is 94659 ---------------------------------------- Adding the digits result 33 Reversing the digits result 95649 Is the number a palindrome? No Is the number prime? No The number is 50424 ---------------------------------------- Adding the digits result 15 Reversing the digits result 42405 Is the number a palindrome? No Is the number prime? No Test 5 numbers to see if they're palindromes... 1. What is the number? 945 945 is not a palindrome. 2. What is the number? 6556 6556 is a palindrome. 3. What is the number? 32574 32574 is not a palindrome. 4. What is the number? 11111 11111 is a palindrome. 5. What is the number? 58685 58685 is a palindrome.
What is the seed value? 1 The number is 71341 ---------------------------------------- Adding the digits result 16 Reversing the digits result 14317 Is the number a palindrome? No Is the number prime? Yes The number is 22417 ---------------------------------------- Adding the digits result 16 Reversing the digits result 71422 Is the number a palindrome? No Is the number prime? No The number is 75961 ---------------------------------------- Adding the digits result 28 Reversing the digits result 16957 Is the number a palindrome? No Is the number prime? No The number is 19769 ---------------------------------------- Adding the digits result 32 Reversing the digits result 96791 Is the number a palindrome? No Is the number prime? No The number is 28216 ---------------------------------------- Adding the digits result 19 Reversing the digits result 61282 Is the number a palindrome? No Is the number prime? No The number is 34093 ---------------------------------------- Adding the digits result 19 Reversing the digits result 39043 Is the number a palindrome? No Is the number prime? No The number is 78188 ---------------------------------------- Adding the digits result 32 Reversing the digits result 88187 Is the number a palindrome? No Is the number prime? No The number is 43995 ---------------------------------------- Adding the digits result 30 Reversing the digits result 59934 Is the number a palindrome? No Is the number prime? No The number is 10684 ---------------------------------------- Adding the digits result 19 Reversing the digits result 48601 Is the number a palindrome? No Is the number prime? No The number is 29525 ---------------------------------------- Adding the digits result 23 Reversing the digits result 52592 Is the number a palindrome? No Is the number prime? No Test 5 numbers to see if they're palindromes... 1. What is the number? 123456 123456 is not a palindrome. 2. What is the number? 1234321 1234321 is a palindrome. 3. What is the number? 77 77 is a palindrome. 4. What is the number? 4343 4343 is not a palindrome. 5. What is the number? 97579 97579 is a palindrome.
What is the seed value? 7 The number is 8221 ---------------------------------------- Adding the digits result 13 Reversing the digits result 1228 Is the number a palindrome? No Is the number prime? Yes The number is 48660 ---------------------------------------- Adding the digits result 24 Reversing the digits result 6684 Is the number a palindrome? No Is the number prime? No The number is 67174 ---------------------------------------- Adding the digits result 25 Reversing the digits result 47176 Is the number a palindrome? No Is the number prime? No The number is 81261 ---------------------------------------- Adding the digits result 18 Reversing the digits result 16218 Is the number a palindrome? No Is the number prime? No The number is 61106 ---------------------------------------- Adding the digits result 14 Reversing the digits result 60116 Is the number a palindrome? No Is the number prime? No The number is 53388 ---------------------------------------- Adding the digits result 27 Reversing the digits result 88335 Is the number a palindrome? No Is the number prime? No The number is 61199 ---------------------------------------- Adding the digits result 26 Reversing the digits result 99116 Is the number a palindrome? No Is the number prime? No The number is 73412 ---------------------------------------- Adding the digits result 17 Reversing the digits result 21437 Is the number a palindrome? No Is the number prime? No The number is 38682 ---------------------------------------- Adding the digits result 27 Reversing the digits result 28683 Is the number a palindrome? No Is the number prime? No The number is 50965 ---------------------------------------- Adding the digits result 25 Reversing the digits result 56905 Is the number a palindrome? No Is the number prime? No Test 5 numbers to see if they're palindromes... 1. What is the number? 945 945 is not a palindrome. 2. What is the number? 6556 6556 is a palindrome. 3. What is the number? 32574 32574 is not a palindrome. 4. What is the number? 11111 11111 is a palindrome. 5. What is the number? 58685 58685 is a palindrome.