Write a program using nested loops which outputs the numbers
Write a program, using nested loops, which outputs the numbers from 1 to 80 in 8 columns and 10 rows. Your output should look something like this. (The sample programs should help you figure out how to do this): Now modify it so it outputs the table like this. Note that the numbers go down the columns now (but you still have to output row by row…). You may need to do some thinking to figure out how to do this; it might help to do it on paper first, and figure out what you need to add as you go across the columns, and as you go down to the next row. (Hint: for each row, what is the value at the beginning of the row, and what do you need to add to each column to get the value for the next column of the current row?) Now modify it so it prompts you for the maximum number to output, and the number of rows. (Keep it simple…no need for error checking on the input.) How many numbers to output? 80 how many rows? 14
Solution
// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int ROW = 10;
int COLUMNS = 8;
cout << \"Type 1\ \";
int number = 1;
// using nested loops,
// outputs the numbers from 1 to 80 in 8 columns and 10 rows
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COLUMNS; ++j)
{
cout << number << \" \";
number++;
}
cout << endl;
}
cout << \"\ \ Type 2\ \";
// using nested loops,
// numbers go down the columns now (but output row by row)
for (int i = 0; i < ROW; ++i)
{
number = i+1;
for (int j = 0; j < COLUMNS; ++j)
{
cout << number << \" \";
number = number + 10;
}
cout << endl;
}
return 0;
}
/*
output:
Type 1
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80
Type 2
1 11 21 31 41 51 61 71
2 12 22 32 42 52 62 72
3 13 23 33 43 53 63 73
4 14 24 34 44 54 64 74
5 15 25 35 45 55 65 75
6 16 26 36 46 56 66 76
7 17 27 37 47 57 67 77
8 18 28 38 48 58 68 78
9 19 29 39 49 59 69 79
10 20 30 40 50 60 70 80
*/

