I am currently trying to write a program that causes Additive Persistance to happen based on the numbers give in an input.txt. I was given the correct outputs beforehand, but I am not getting them in the last 3 inputs. Below is my code and also the outputs that are suppose to happen. Any insight would be greatly appreciated, thanks!
#include <iostream>
#include <fstream>
int AdditiveP(int number) {
int sum = 0;
int i;
for (i = number; i > 0; i /= 10)
sum += i % 10;
return number == sum ? 0 : (1 + AdditiveP(sum));
}
int main() {
std::ifstream infile("input.txt");
int number;
int sum = 0;
while(infile >> number) {
int numsum = number;
sum =+ (numsum % 10);
numsum = numsum/10;
if (number >= 0) {
std::cout << AdditiveP(number) << " " << sum << std::endl;
}
else {
break;
}
}
}
The outputs I should be getting are:
0 3
0 8
0 9
2 1
3 1
2 9
#include <iostream>
#include <fstream>
usingnamespace std;
int AdditiveP( int &number )
{
int sum = 0;
for ( int i = number; i > 0; i /= 10 ) sum += i % 10;
if ( number == sum )
{
return 0;
}
else
{
int value = 1 + AdditiveP( sum );
number = sum;
return value;
}
}
int main()
{
int number;
// std::ifstream infile("input.txt");
// while ( infile >> number )
while ( true )
{
cout << "Enter a number: "; cin >> number;
if ( number >= 0 )
{
int P = AdditiveP( number );
cout << P << " " << number << endl;
}
}
}
Enter a number: 8
0 8
Enter a number: 123
1 6
Enter a number: 12345
2 6