Question

How to write a C++ program. Additive persistence is a property of the sum of the...

How to write a C++ program.

Additive persistence is a property of the sum of the digits of an integer. The sum of the digits is found, and then the summation of digits is performed creating a new sum. This process repeats until a single integer digit is reached. Consider the following example:

1. The beginning integer is 1234

2. Sum its digits is 1+2+3+4 = 10

3. The integer is now 10

4. The sum of its digits is 1 + 0 = 1

5. The integer is 1. When the value reaches a single digit, we are finished. This final integer is the additive root

Program Specifications

The program should run as follows.

1) Gather input as a redirection from a file input.txt. The program ends under one of the following circumstances a. the next gathered integer is a negative number. b. if all the integers from the file have been processed.

2) If the given integer is a single digit, report its additive persistence as 0 and its additive root as itself on a single line as two space separated numbers

3) For each multidigit, non-negative, integer output on a single line the two space separated numbers: a. the integer's additive persistence b. the integer's additive root

How to write a C++ program.

Homework Answers

Answer #1

Code

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

void digSum(int n)
{
   int sum = 0;
  
   while(n > 0 || sum > 9)
   {
       if(n == 0)
       {
           n = sum;
           sum = 0;
       }
       sum += n % 10;
       n /= 10;
   }
   if(n==sum)
   cout <<"0 "<<sum<<endl;
   else
   cout <<"1 "<<sum<<endl;
}

int main() {

   //Create a dynamic array to hold the values
   vector<int> numbers;

   //Create an input file stream
   ifstream in("/Users/Akhilaperumalla/Desktop/readme.txt",ios::in);

   /*
As long as we haven't reached the end of the file, keep reading entries.
   */

   int number; //Variable to hold each number as it is read
  
//Read number using the extraction (>>) operator
while (in >> number) {
       //Add the number to the end of the array
       numbers.push_back(number);
   }

   //Close the file stream
   in.close();

   /*
   Now, the vector<int> object "numbers" contains both the array of numbers,
and its length (the number count from the file).
   */

   //Display the numbers
   cout << "Numbers:\n";
   for (int i=0; i<numbers.size(); i++) {
       if(numbers[i] < 0)
           break;
       else
           digSum(numbers[i]);
   }

   //cin.get(); //Keep program open until "enter" is pressed
   return 0;
}

Screenshot of input file

Screenshot of output:

Know the answer?
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
Similar Questions
Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in...
Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in a few lines. In a single file view to create a list of these numbers and find the arithmetic mean of the list elements. The resulting value is recorded in a text file OUTPUT.TXT.
Write a C++ program to read a positive integer greater than 0, the program should compute...
Write a C++ program to read a positive integer greater than 0, the program should compute and display the following: - Sum of odd digits in the number - Count of even digits in the number - The smallest digit.                    For example, if the input is 24536, then Sum of odd digits in the number = 8 Count of even digits in the number = 3 Smallest digit = 2
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line...
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum...
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum of two preceding: 0, 1, 1, 2, 3, 5, 8, 13, 21 ..... Your program should ask user for the integer, and output the result, which shows Fibonacci number at that position. For example if user enters '5', program should output '3' (fifth position in the series). Your program should use recursive function to compute and return back to the main the Fibonacci number....
given an integer, write a program in C++ that displays the number as follows first line        ...
given an integer, write a program in C++ that displays the number as follows first line         all digits second line   all except first digit ... last line          last digit ex) 5 6 7 8 6 7 8 7 8 8
● Write code to read the content of the text file input.txt using JAVA. For each...
● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is...
 Write a program to find the prime numbers - Ask user to input the integer...
 Write a program to find the prime numbers - Ask user to input the integer number - test the number whether it is a prime number or not - Then, print “true” or “false” depending on whether the number is prime or isn’t. - Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. - Use idea of user input, cumulative sum,...
Write in C++: Write a program to convert a text-file containing expressions (one per line) into...
Write in C++: Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted to a file of your choice using a stack with one space between operators and variables ( one letter variables) and/or constants (one digit constants).
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by...
JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is -3 10...