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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

#include <iostream>
using namespace std;
int i;
int main()
{
// This variable 'n' will be used repeatedly for how far any loop will count
// You may define additional variables as necessary when solving the problems.
int n;
// These variables will hold the sums and products of selected values
int sum, product=0;
cout << "Please enter a small positive integer: ";
cin >> n;
// The first couple of these operations will be repeated for various values of n
while (n > 0)
{
// STEP 1:
// Write a loop that displays the sum of the first 'n' integers
// For example, the sum of the first three integers is 6.
sum=0;
for (i=1;i<=n;i++)
sum+=i;
cout << "The sum of the first " << n << " integers is " << sum << "." << endl;
// STEP 2:
// Write a loop that displays the first 'n' integers that are NOT multiples of 3
// For example, the first 5 such numbers are: 1 2 4 5 7
// HINT: The largest such integer will be just less than 1.5*n
// Display all these values on a single line, separated by spaces.
for (i=1;i<=1.5*n;i++)
if(i%3!=0)
cout << i << " ";
cout << endl; // end the line After all the values display
// STEP 3:
// Write a loop that displays the sum of the first 'n' odd integers
// The largest such integer will be just less than 2*n
// FOr partial credit, it will suffice to get the correct sum
// For full credit, do so without using any if statements to test a number is odd
sum=0;
for (i=1;i<2*n;i+=2)
sum+=i;
cout << "The sum of the first " << n << " odd integers is " << sum << "." << endl;
// STEP 4:
// Complete the loop that started on line 36 by prompting for
// and accepting a new input value. Tell the user how to stop!
cout << "Please give a new value. If you want to stop give 0." << endl;
cin >> n;
cout << endl;
}
// STEP 5:
// Test the code that you have so far
// STEP 6:
// For any positive integer n, n factorial (written as 'n!' in math)
// is the product of all the positive integers up to and including n.
// Write a program loop that displays the first nine factorials
// The first three lines of 1! = 1
// output should look like 2! = 2
// those at the right here 3! = 6
// For full credit, do so with a single loop (no nested loops)
product=1;
for(i=1;i<=9;i++)
{
product=product*i;
cout << i << "! = " << product << endl;
}
cout << endl;
// STEP 7:
// Now produce the same output, but in reverse order, starting with 9 factorial
// Again, for full credit, do so with a single loop (no nested loops)
// Hint: you may use the results from the end of the previous loop
cout << i1 << "! = " << product << endl;
for(i=9;i>=2;i)
{
product=product/i;
cout << i1 << "! = " << product << endl;
}
cout << endl;
// STEP 8:
// Test your results of the code you have so far.
// The factorial answers should match each other.
return 0;
}
