-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathEx5_04.cpp
More file actions
25 lines (21 loc) · 791 Bytes
/
Ex5_04.cpp
File metadata and controls
25 lines (21 loc) · 791 Bytes
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
// Multiple initializations in a loop expression
import <iostream>;
import <format>;
int main()
{
unsigned int limit {};
std::cout << "This program calculates n! and the sum of the integers "
<< "up to n for values 1 to limit.\n";
std::cout << "What upper limit for n would you like? ";
std::cin >> limit;
// The format string for all rows of the table
const auto table_format{ "{:>8} {:>8} {:>20}\n" };
// Output column headings
std::cout << std::format(table_format, "integer", "sum", "factorial");
for (unsigned long long n {1}, sum {}, factorial {1}; n <= limit; ++n)
{
sum += n; // Accumulate sum to current n
factorial *= n; // Calculate n! for current n
std::cout << std::format(table_format, n, sum, factorial);
}
}