Skip to content

Instantly share code, notes, and snippets.

@CarrCodes
Created April 3, 2018 17:32
Show Gist options
  • Save CarrCodes/daa2b3393a93f8d279be73216995a69e to your computer and use it in GitHub Desktop.
Save CarrCodes/daa2b3393a93f8d279be73216995a69e to your computer and use it in GitHub Desktop.
// Author: Taylor Carr
//
// Spring 2016
//
// This program utilizes recursive functions to manipulate
// an integer value entered by the user.
#include <iostream>
#include <cmath>
using namespace std;
void displayMenu(); // Displays menu choices
int getNum(); // Gets integer from user
bool increasing(int number, bool order); // Checks to see if the individual
// numbers in the integer are in
// increasing order
void reverse(int number, int temp); // Displays the integer in reverse
int sum(int number, int total); // Adds up the individual numbers in
// the integer
long squareSum(int number, long sumOfSquares); // Adds up the squares of
// every number from 0 to n
void vertical(int number, int temp); // Displays the digits of the integer
// vertically
int main(){
char answer;
bool running = true;
cout << "*** Welcome to My Program Using Recursions ***\n"
"\tThe function of this program is to\n"
"\taccept a positive integer that is > 9 from the keyboard.\n"
"\tThe program then does the following:\n"
"\t 1. Returns true if the digits of that integer n\n"
"\t are in increasing order; otherwise ,\n"
"\t the function returns false.\n"
"\t 2. Returns the numbers with the digits reversed.\n"
"\t 3. Returns the sum of the digits of the integer.\n"
"\t 4. Returns the sum of squares of the numbers from 0\n"
"\t to the number n.\n"
"\t 5. Displays the number vertically.\n";
while(running == true) {
int number = -1,
temp = 0,
total = 0;
long sumOfSquares = 0;
bool inOrder = true;
displayMenu();
cin >> answer;
switch(answer){
case '1':
number = getNum();
if (number != -1){
inOrder = increasing(number, inOrder);
if (inOrder == true){
cout << endl << "The digits of " << number
<< " are in increasing order.\n";
}
else {
cout << endl << "The digits of " << number
<< " are not in increasing order.\n";
}
cout << endl << "The original digits are " << number
<< " --- Digits Reversed = ";
reverse(number, temp);
cout << endl;
total = sum(number, total);
cout << endl << "Sum of the digits of the number "
<< number << " is = " << total << endl;
sumOfSquares = squareSum(number, sumOfSquares);
cout << endl << "Sum of squares from 0 to " << number
<< " = " << sumOfSquares << endl;
cout << endl << number << " Displayed Vertically" << endl;
vertical(number, temp);
}
break;
case '9':
running = false;
break;
default:
cout << endl << "Invalid Choice\n";
break;
}
}
cout << endl
<< "Taylor Carr - Tweak Programming Director\n"
"April 2016";
return 0;
}
// Displays menu choices
void displayMenu(){
cout << endl
<< "Select from the following menu\n"
"1. Enter a positive integer > 9.\n"
"9. Terminate the program. ";
}
// Gets integer from user
int getNum(){
int number;
cout << endl << endl
<< "Enter a positive integer > 9. ";
cin >> number;
if (number < 10){
cout << endl << "Invalid Number - Number must be > 9\n";
number = -1;
}
return number;
}
// Checks to see if the individual numbers in the
// integer are in increasing order
bool increasing(int number, bool inOrder){
int temp;
if (inOrder == false){
return inOrder;
}
else if (inOrder == true && number < 10){
return inOrder;
}
else {
temp = number % 10;
number/=10;
if (temp < number % 10){
inOrder = false;
}
return increasing(number, inOrder);
}
}
// Displays the integer in reverse
void reverse(int number, int temp){
if (number >= 1){
temp = number % 10;
cout << temp;
reverse(number/10, 0);
}
}
// Adds up the individual numbers in the integer
int sum(int number, int total){
if (number < 1){
return total;
}
else {
total = total + (number % 10);
return sum (number / 10, total);
}
}
// Adds up the squares of every number from 0 to n
long squareSum(int number, long sumOfSquares){
if (number == 0){
return sumOfSquares;
}
else {
sumOfSquares = sumOfSquares + pow(number, 2);
return squareSum(number - 1, sumOfSquares);
}
}
// Displays the digits of the integer vertically
void vertical(int number, int temp){
if (number >= 1){
temp = number % 10;
vertical(number/10, 0);
cout << temp << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment