Question

Hello, I am having trouble with this C++ programming assignment. If someone could write the code...

Hello, I am having trouble with this C++ programming assignment. If someone could write the code or at least part of it that would help me, because every code I try has errors.

Using if Statements, Loops and Nested Loops • Scanning Characters in a String Using a Loop • Performing Character Arithmetic In project, you will write an interactive program that, counts the number of digits in a non-negative integer, factorizes the integer into powers of ten and its digits, computes the sum of its digits and counts the number of odd digits that it has. The output of the program will be formatted as shown in the sample runs. Reuse variables where possible and avoid declaring variables for values that are calculated and used only once.

Programming Tips and Suggestions

1. Use the standard C++ math library log10 function to determine the number of digits, d, in a positive integer, n. The number of digits in n is given by the expression d=static cast< int >(log10(n))+1. The place value of the most significant digit is 10d−1.

2. Use a loop to peel off the digits of an integer beginning with the most significant digit. Each non-zero digit should be expressed as a power of 10 as shown in the sample run. Observe that the most significant digit of a non-negative integer n is n/p, where p is the place value of the most significant digit. To obtain the second most significant digit, divide the number formed by peeling off the most significant digit from the number by the place value of its most significant digit. Use the pow only to obtain the most significant digit of the original number.

3. The digits should be summed and the number of odd digits in the number should be counted. Use the modulus operator when counting odd digits rather than an if statement that checks whether the digits 1, 3, 5, 7 or 9.

4. An if statement will be required to avoid printing a power of 10 when the digit is a 0.

5. Only one loop should be used for this program.

6. When factorizing the integer into powers of 10 using its digits, you may want to deal with three separate cases: 1-digit, 2-digit and 3-or-moredigit cases. For the 3-or-more-digit case, process the first digit before the loop, process all but the last two digits within the loop and the last two digits after the loop.

7. Program incrementally. Get the program to work for one integer before adding a loop for it to work for any number of integers. First, get the factorization to work, then the summing of the digits and then the counting of the odd digits.

Here is a hand-trace of how to obtain the digits of a non-zero number. It consists of alternating the integer division and integer modulo operations while updating the place value, by dividing it by 10, as digits are extracted from the number from the most significant to the units digit. number = 962357 number of digits = 6 Most significant digit’s place value = 10^(6-1) = 10^5 = 100000 Digit Place Value Remainder 962357/100000 = 6 10^5 962357 (mod 100000) = 62357 62357/10000 = 6 10^4 62357 (mod 10000) = 2357 2357/1000 = 2 10^3 2357 (mod 1000) = 357 357/100 = 3 10^2 357 (mod 100) = 57 57/10 = 5 10 57 (mod 10) = 7 7 1

Here are a few sample runs.

Listing 1: A Sample Run 1 Enter an integer -> -2 The integer must be non-negative.

Listing 2: A Sample Run 2 Enter an integer -> 104529 104529 = 1 x 10^5 + 4 x 10^3 + 5 x 10^2 + 2 x 10 + 9 sumOfDigits (104529) = 21 #OddDigits (104529) = 3

Listing 3: A Sample Run 3 Enter an integer -> 1005 1005 = 1 x 10^3 + 5 sumOfDigits (1005) = 1 + 0 + 0 + 5 = 6 #OddDigits(1005) = 2

Listing 4: A Sample Run 4 Enter an integer -> 7654321 7654321 = 7 x 10^6 + 6 x 10^5 + 5 x 10^4 + 4 x 10^3 ← + 3 x 10^2 + 2 x 10 + 1 sumOfDigits (7654321) = 28 #OddDigits (7654321) = 4

Listing 5: A Sample Run 5 Enter an integer -> 129 129 = 1 x 10^2 + 2 x 10 + 9 sumOfDigits(129) = 12 #OddDigits(129) = 2

Listing 6: A Sample Run 6 Enter an integer -> 5 5 = 5 sumOfDigits(5) = 5 #OddDigits(5) = 1

Listing 7: A Sample Run 7 Enter an integer -> 20 20 = 2 x 10 sumOfDigits(20) = 2 #OddDigits(20) = 0 Listing 8: A Sample Run 8 Enter an integer -> 0 0 = 0 sumOfDigits(0) = 0 #OddDigits(0) = 0 Listing 9: A Sample Run 9 Enter an integer -> 10100 10100 = 1 x 10^4 + 1 x 10^2 sumOfDigits (10100) = 2 #OddDigits (10100) = 2

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

Thanks for the question, here is the complete code in C++. Hope it helps you. Let me know for any help with any other questions. Thanks a lot, do give a thumbs up please : )

====================================================================

#include<iostream>
#include<cmath>

using namespace std;

int main(){
  
   int n;
   printf("Enter an integer: "); cin>>n;
   if(n<0){
      cout<<"The integer must ne non-negative.";
       return 0;
   }  
   int digits=static_cast<int>(log10(n))+1;
  
   int digitSum =0;
   int divisor=0;
   cout<<n<<" = ";
   int temp=n;
   int oddDigits=0;
   for(int digit=digits-1; digit>=0; digit--){
       divisor = static_cast<int>(pow(10,digit));
       int msd = n/divisor;
       n = n%divisor;
       cout<<msd<<"x"<<10<<"^"<<digit;
       if(digit!=0) cout<<" + ";
       digitSum+=msd;
       oddDigits += msd%2==1?1:0;
   }
   cout<<"\nSum of Digits ("<<temp<<") = "<<digitSum<<endl;
       cout<<"Odd Digits ("<<temp<<") = "<<oddDigits<<endl;
  
  
}

====================================================================

Add a comment
Know the answer?
Add Answer to:
Hello, I am having trouble with this C++ programming assignment. If someone could write the code...
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
  • In Python: Write a well-documented (commented) program that asks the user for a 9-digit integer, computes...

    In Python: Write a well-documented (commented) program that asks the user for a 9-digit integer, computes its checksum, and then prints the corresponding ISBN number. The International Standard Book Number (ISBN) is a 10-digit code that uniquely specifies a book. The rightmost digit is a checksum digit that can be uniquely determined from the other 9 digits, from the condition that d1 + 2d2 +3d3 + ... + 10d10 must be a multiple of 11 (here di denotes the ith...

  • In Python Write a well-documented (commented) program that asks the user for a 9-digit integer, computes...

    In Python Write a well-documented (commented) program that asks the user for a 9-digit integer, computes its checksum, and then prints the corresponding ISBN number. The International Standard Book Number (ISBN) is a 10-digit code that uniquely specifies a book. The rightmost digit is a checksum digit that can be uniquely determined from the other 9 digits, from the condition that d1 + 2d2 +3d3 + ... + 10d10 must be a multiple of 11 (here di denotes the ith...

  • Here is what i have so far... However i am having a little bit of trouble...

    Here is what i have so far... However i am having a little bit of trouble with it. when inputting a number the sum comes out perfect but when i break the digits up i cant do mmore than 4 digits and i cant do a negitive number... someone help. QUESTION: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example,...

  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • Write a c++ program that reads numbers from the user, and prints a table showing how...

    Write a c++ program that reads numbers from the user, and prints a table showing how many times each digit appears in each number. The user should be able to enter more than one number to be tested for repeated digits. The program should terminate when the user enters a number that is less than 0. Here is a sample output: Enter a number (-1 to end this loop): 41271092 Digit: 0 1 2 3 4 5 6 7 8...

  • Please I need help with this c++ code. please show all steps , write comments and...

    Please I need help with this c++ code. please show all steps , write comments and show sample runs. Thank you. 1. The Federal Bureau of Investigation (FBI) has recently changed its Universal Control Numbers (UCN) for identifying individuals who are in the FBI's fingerprint database to an eight-digit base 27 value with a ninth check digit. The digits used are: 0123456789ACDE FHJKLMNPRTVWX Some letters are not used because of possible confusion with other digits: B->8, G->C, I- >1, 0->0,...

  • Write C++ program T 1030 UUIII DUCOUL The bar code on an envelope used by the...

    Write C++ program T 1030 UUIII DUCOUL The bar code on an envelope used by the US Postal Service represents a five (or more) digit zip code using a format called POSTNET (this format is being deprecated in favor of a new system, OneCode, in 2009). The bar code consists of long and short bars as shown below: Illlllllllll For this program we will represent the bar code as a string of digits. The digit 1 represents a long bar...

  • I need it in c++ The U.S. Banking System The code assigned to a bank is...

    I need it in c++ The U.S. Banking System The code assigned to a bank is an eight digit number plus a ninth check digit. To check for validity, the first eight digits are assigned weights (left to right) of 7, 3, and 9 repetitively. Each digit is multiplied by its weight and the products summed. The resulting sum (mod 10) should equal the check digit. The assigning of weights insures that all single digit errors and most transposition errors...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

  • For programming C language This problem will be briefly discussed in Lab2 and the TĀ will...

    For programming C language This problem will be briefly discussed in Lab2 and the TĀ will give you hints to solve it. However, the TĀ will not write the code. This code is a bit complicated, so try to understand the process from the TA so that you can continue working on it at home. Reverse the Number Write a program that takes a 5-digit number as input and print the reverse of the number. Sample Input/Output: Enter a five...

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
ADVERTISEMENT