I will provide you with a code for the problem in c++. The code is quite is self-explanatory with just a twist. The twist is that since the size of the numbers gradually increases I have used the built-in "string" data structure in c++ to store the numbers instead of using integers or long integers. It is better this way since it will work for much larger numbers and also writing the code is much easier. I have written the code in a way easier for you to understand, just give it a read and I hope you will understand how it works. One important thing is that do remember to run the code in higher versions of c++ (preferably c++11) if you are using Linux use the flag --std=c++11. If you have any problem understanding the code leave down a comment below about what is bothering how and I will be more than happy to help you out. Now here goes the code:-
#include <bits/stdc++.h> // imports all necessary headers
using namespace std;
int main()
{
string num;
int iter;
cin >> num >> iter;
for(int i = 1; i <= iter; i++) {
int len = num.size();
num += '?';
string temp = "";
int count = 0;
for(int j = 0; j < len; j++)
{
if(num[j] ==
num[j + 1])
count++;
else {
count++;
temp += to_string(count);
temp += num[j];
count = 0;
}
}
cout << "iteration no "
<< i << " -- " << temp << endl;
num = temp;
}
return 0;
}
Thank you. Happy Coding.
#include <bits/stdc++.h>
int main()
{
string num;
int iter;
cin >> num >> iter;
for(int i = 1; i <= iter; i++) {
int len = num.size();
num += '?';
string temp = "";
int count = 0;
for(int j = 0; j < len; j++) {
if(num[j] == num[j + 1])
count++;
else {
count++;
temp += to_string(count);
temp += num[j];
count = 0;
}
}
cout << "iteration no " << i << " -- " << temp << endl;
num = temp;
}
return 0;
}
C++ Write a program that reads in two Integers. A starting number and a number of iterations. For each iteration, you ge...