Question

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.

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:**

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

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, 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 all
digits
second line all except first digit
...
last line
last digit
ex)
5 6 7 8
6 7 8
7 8
8

4) Write a Java program using Conditions: Write a program where
it will ask user to enter a number and after that it will give you
answer how many digits that number has.
Steps: 1) Create Scanner object and prompt user to enter the
number and declare variable integer for input
2) Use if else condition with && condition where you
will specify the digits of numbers by writing yourself the digit
number that should display: Example(num<100 &&
num>=1), and...

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 = ', 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 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 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...

Write a Java program that gets a 4x4 array of numbers and that calculates the sum and average of numbers on the main diagonal, ie the diagonal that goes from the left corner down to the right corner.
Examples of resulting programs are:
Enter a 4x4 matrix row by row:
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
The sum of numbers in the diagonal is: 16
The average of numbers in...

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 22 minutes ago

asked 24 minutes ago

asked 29 minutes ago

asked 29 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 2 hours ago

asked 2 hours ago