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 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 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...
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
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
a) sum = 0 for letter in '12345' : sum = sum + int(letter) print('sum =...
a) sum = 0 for letter in '12345' : sum = sum + int(letter) print('sum = ', sum) Write a program which reads a string containing any characters from the user and prints the sum of the digits in the string. For example, if the string from the user is "I want 3 oranges and 24 bananas', then the output would be 9, since 3 + 2 + 4 = 9. Note that a character C is a digit if...
write a C program to find whether the given number of three digits is an integer...
write a C program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or -999 to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or -999 to quit : 371 3**3 + 7**3 + 1**3 is = 371...
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
C program question: Write a small C program connect.c that: 1. Initializes an array id of...
C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to...
1. Write a program to: Prompt the user to input an integer, a double, a character,...
1. Write a program to: Prompt the user to input an integer, a double, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space. Add a statement to also output in reverse. Add a statement to cast the double to an integer, and output that integer. 2. 2. Choose the statement(s) that generate(s) this output: I wish you were here a. printf("I wish", "you were here"); b....
Write a MASM program that computes the sum of the integers from 1 to N where...
Write a MASM program that computes the sum of the integers from 1 to N where N is a positive integer. Use the equal sign directive to define N. Save the sum in the EAX register. You must use loops. For example, 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Language ( Assembly)...