Write a brief piece of code (not a complete program) that displays the numbers 1 through n on a screen, with only 5 numbers per line. The numbers should be spaced evenly as shown below for the case of n=19:
I am assuming we need to use while loops
int number; // Our variable for max count
int n; // If you need to dynamically space
int padding = 0; // for dynamic spacing,
// otherwise set as 2
cin >> number;
n = number;
//to figure out how much to pad so as to equally format
do {
int digit = n % 10;
padding++;
n /= 10;
} while (n > 0);
//output the numbers
for(int i = 1; i <= number; i++)
{
cout << "n=" << setw(padding) << i;
if(i%5 == 0)
cout << endl;
}
I think this is what you need, not exactly sure. The biggest problem people have in programming classes is reading & problem solving.