Question

Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices....

Objectives: Integer arithmetic, Functions, menus.

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:

Page 1 of 4

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 positiveintegers. 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
//method to count the number of digits of a number
int getNumDigits(int number)
{
   int c=0,r;
   //loop will continue till the number !=0
   while(number!=0)
   {
      r=number%10;
      c++; //count the number of digits
      number=number/10; //reduce the number
   }
   return c; //return the count
}
//method to return the nth position digit of a number
int getNthDigit(int number,int n)
{
int c=0,r;
//loop will continue till the number !=0
    while(number!=0)
   {
      r=number%10;
      c++; //count the number of digits of a number
      if(c==n) //if value of c and n equal then return the digit i.e/ r
      return r;
      number=number/10; //reduce the number
   }
   return 0;
}
//returns the sum of digits of a number
int getSumDigit(int number)
{
   int sum=0,r;
   //loop will continue till the number !=0
    while(number!=0)
   {
      r=number%10; //fidn the individual digits of a number
      sum=sum+r; //find the sum
       number=number/10;//reduce the number
   }
   return sum; //return the sum
  
}
//method to check a number a spalindrome or not
bool isPalindrome(int number)
{
    int n,rev=0,r;
    n=number;
    //loop will continue till the number !=0
    while(number!=0)
   {
      r=number%10; //find the individual digit of a number
      rev = rev*10 + r; //fidn the reverse of a number
       number=number/10;//reduce the number
   }
   if(rev==n) //compare the reverse and number if equal then return true otherwise return false
   return true;
   else
   return false;
}
//method to return a +ve integer
int getInteger()
{
   int n;
   //loop will terminate when user enter a +vce integer
   do
   {
      cout<<endl<<"Enter a +ve integer";
      cin>>n; //read the value
       }while(n<=0); //if -ve then loop will continue
       return n; //return the n
   }
   //method to control the menu()
   void menu()
   {
      int opt,n,No;
      //infinite loop
      while(1)
      {
         //display the menu
             cout<<endl<<" 1. Find the number of digits in an integer";
      cout<<endl<<" 2. Find the Nth digit in an integer";
      cout<<endl<<" 3. Find the sum of all digits of an integer";
      cout<<endl<<" 4. Is the integer a palindrome ? ";
      cout<<endl<<" 5. Quit";
      cout<<endl<<"Enter your choice";
      cin>>opt; //read the choice
      if(opt==1) //block for find the number of digits of a number
      {
         n=getInteger(); //read the +ve integer
         //display the number of digits
         cout<<endl<<"The number of digits in "<<n<<" is :"<<getNumDigits(n);
       }
       else
       if(opt==2)
       {
          n=getInteger(); //read the +ve integer
         
          cout<<endl<<"Enter the value of which position digit you want to print";
          cin>>No; //read the position
          if(No>getNumDigits(n)) //check the validity of the position
          cout<<endl<<"Your Position exceeds to the number of digits in the number";
          else
          { //if position is valid then return the digit of that position
             cout<<endl<<"The "<<No<<" th Digit of "<<n<<" is "<<getNthDigit(n,No);
               }
              
       }
       else
       if(opt==3)
       {
           n=getInteger(); //read the +ve integer
           //display the sum of digits of the number
           cout<<endl<<"Sum of digits of "<<n<<" is "<<getSumDigit(n);
           }
           else
           if (opt==4)
           {
              n=getInteger(); //read the +ve integer
              if(isPalindrome(n)) //check palindrome
              cout<<endl<<n<<" is palindrome";
              else
              cout<<endl<<n<<" is not a palindrome";
           }
           else
           if(opt==5)
           exit(0); //quit the program
           else
           cout<<endl<<"Invalid Choice"; //invalid choice
      
       }
     
     
   }
   int main()
   {
      //call to menu() to control the program
      menu();
   }

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • (Palindrome integer) Write the functions with the following headers: # Return the reversal of an integer,...

    (Palindrome integer) Write the functions with the following headers: # Return the reversal of an integer, e.g. reverse(456) returns # 654 def reverse(number): # Return true if number is a palindrome def isPalindrome(number): Use the reverse function to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.

  • Write a C++ program to run a menu-driven program with the following choices: Count number of...

    Write a C++ program to run a menu-driven program with the following choices: Count number of even digits in a number Compute the factorial of a number Quit Make sure your program conforms to the following requirements: 1. This program should be called playingWithNumbers.cpp 2. Write a function called getValidUserInputPosNumGT0 that allows a user to enter an integer and validated that the number is > 0. It must have a pass by reference parameter that will store the value selected...

  • Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays,...

    Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...

  • Write C++ program, program should include at least the following functions:  getComputerGuess(): this function should...

    Write C++ program, program should include at least the following functions:  getComputerGuess(): this function should return a random value generated by the computer using rand function ( read more about random numbers in Chapter 3 pages 126 – 128 ).  getUsersChoice(): this function will ask the user to enter a choice using a menu. It then returns the user’s selected choice. Here is the menu: “1) Rock 2)Paper 3) Scissors 4) Quit” You should allow the game to...

  • C# Design and implement a program (name it PalindromeInteger), to check if an integer value is...

    C# Design and implement a program (name it PalindromeInteger), to check if an integer value is a palindrome or not, using 2 methods (reverse and isPalindrome) specified as follows: Method reverse(int number) takes integer number and returns number in reverse order. The method mathematically reverses the number. Method isPalindrome(int number) takes integer number and returns true if the number is palindrome; false otherwise. Use method reverse() to implement method isPalindrome(). Notice that an integer value is palindrome if the value...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • C++ Design and implement a program (name it PalindromeInteger), to check if an integer value is...

    C++ Design and implement a program (name it PalindromeInteger), to check if an integer value is a palindrome or not, using 2 methods (reverse and isPalindrome) specified as follows: Method reverse(int number) takes integer number and returns number in reverse order. The method mathematically reverses the number. Method isPalindrome(int number) takes integer number and returns true if the number is palindrome; false otherwise. Use method reverse() to implement method isPalindrome(). Notice that an integer value is palindrome if the value...

  • Write a program that displays a menu on the screen. The menu will give the user...

    Write a program that displays a menu on the screen. The menu will give the user four options - enter two integers, enter two decimal numbers (#.#), enter one integer and one decimal number, or quit. The inputs should be labelled a, b, c, or d, and you should enter in the letter to choose the appropriate option. Once the user selects the option, two numbers will be read in from the user. The numbers will be added together and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT