Question

In: Other

C++ Write a program that reads in two Integers. A starting number and a number of iterations. For each iteration, you generate a new number that is the result of reading off the number of same digits in the original number. output each Iteration

C++

 

Solutions

Expert Solution

 

#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;
}


Related Solutions

Write a simple C program to generate a chart of circle properties. Output chart “iteration” is...
Write a simple C program to generate a chart of circle properties. Output chart “iteration” is to be specified by user input. Your chart will include the radius, diameter, circumference, and Area values in a single table, and radius values will range from 0 to 50, floating point values, with three-decimal-place precision. Table should have all column entries right-justified and each column will have a heading above the entries. For example, assuming user enters a incrementation value of 5, the...
Write a C program whose input is two integers, and whose output is the first integer...
Write a C program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. For coding simplicity, output a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT