Starting from:

$30

CS2400-Homework 4 Integer,Arithmetic,Functions and Menus Solved

Write   a C++ program that displays the following  menu   of               choices.                           

              

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?  

5.  Quit

Enter a choice:

              

For each of the choices (1, 3, 4), Read a positive integer number and call a function that processes the menu choice (descriptions below).  For option 2 read two positive integers. If the n value is invalid, print an error message and display the menu again. Your program should continuously display the menu until the user enters 5 to quit.  Any other choice should be ignored and display the menu again.

 

Required Functions (use the provided prototype and function names):

•        Option 1, int getNumDigits(int number):

            Return the number of digits in number. 

 

•        Option 2, int getNthDigit(int number, int n):

            Return the digit number specified by n. Right most digit is digit one.

 

•        Option 3, int getSumDigits(int number):

            Return the total value of all the digits.

 

•        Option 4, bool isPalindrome(int number):

returns true if the number is a palindrome (1, 121, 1221, 134431, etc.). False otherwise.

 

•        A function (int getInteger()) that returns a positive integer. This function should loop until the user enters a positive integer. 

 

All of the above functions must only manipulate integers.  Do not use string types or arrays in any part of this project.

 

 

 

 

 

             

Sample Run (user input in red):

 

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 0

 

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 1

Enter a positive integer: -9

Enter a positive integer: -7

Enter a positive integer: 123

 

123 has 3 digits.

 

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 2

 

Enter an integer number: 23178149  
Enter a position: 4

 

Digit number 4 is 8.

 

1.  Find the number of digits in an integer.
2.  Find the nth digit in an integer.
3.  Find the sum of all digits of an integer.
4.  Is the integer a palindrome?
5. Quit

Enter a choice: 2

 

Enter an integer number: 23  

Enter a position: 4

 

Invalid position.

 

                

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 3

Enter an integer number: 123456789

 

The sum of all digits in 123456789 is 45.

 

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 4

Enter an integer number: 1

 

The number 1 is a palindrome.

 

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 4

Enter an integer number: 12

 

The number 12 is not a palindrome.

 

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 4

Enter an integer number: 1223221

 

The number 1223221 is a palindrome.

 

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 4

Enter an integer number: 1221

 

The number 1221 is a palindrome.

 

1.  Find the number of digits in an integer.

2.  Find the nth digit in an integer.

3.  Find the sum of all digits of an integer.

4.  Is the integer a palindrome?

5.  Quit

Enter a choice: 5

More products