Help Please

If someone could please help me with this program

1. Write a C program that prints numbers from 1 to 104, each 8 numbers on a separate line, at the end of each line find the sum of its numbers. Use only one if statement inside a while loop statement.

For example,
1 2 3 4 5 6 7 8 a sum of 36
9 10 11 12 13 14 15 16 a sum of 100
.
.
97 98 99 100 101 102 103 104 a sum of 804

(Hint: compare the result of modulus 8 to zero)


2. The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined as follows:
n! = n .(n-1) . (n-2). …… .1 (for values of n greater than or equal to 1)
and
n! =1 (for n =0).
For example, 5! = 5 . 4 . 3 . 2 . 1, which is 120.

2.a) [30] Write a C program to calculate n!

2.b) [30] Write a C program to calculate F=

2.c) [40] Write a C program that estimates the value of the mathematical constant e by using the formula:


Sorry I didnt notice for 2b its suppose to be F=1/1 + 1/2+1/3....+1/10

and for 2c the formula is e=1 + 1/1! + 1/2!+.....+1/10!


Thank you in advance

1.

You need to start at 1, so initialize a value, let's say i, to that value. Your while loop should go up to and including 104. After printing the first eight values, i will be eight. Increment it, and i = 9. At this point, we need to sum the last eight values and go to a new line. To sum of the first n integers is n*(n + 1)/2. If you don't know that formula, check out this link:
http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/runsums/triNbProof.html

However, in our case, i is 9 and we only want to sum the first 8 integers. So subtract 1 from each term in the numerator. Also, we don't want to sum all of the integers up to that point, just the last eight. So, subtract the sum of all integers before this point. You could probably simplify the expression I've used, but it's just the first one that came to mind.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    while (i <= 104)
    {
        cout << i << " ";
        i ++;
    if ((i - 1) % 8 == 0)
    {
        cout << " a sum of " << (i*(i - 1)/2) - ((i - 8)*(i - 9)/2) << endl;
    }
    }
}


2.a) You'll need to use another while loop this time (or you could use a for loop). Initialize a result = 1 and a value i = n. Each time through the loop, multiply result*i, then decrement i until you get to one. Alternatively, you could start the while loop with i = 1 and go up to i = n.

2.b) Are you getting the hang of loops yet? For this one, your counter i should go from one to ten. Start with a result = 0. Each time through the loop, add 1/i to the result.

2.c) I'll leave this one completely up to you. It's basically just a combination of 2.a) and 2.b).
Last edited on
-should be adding 1 to i
-should subtract 7 from i not 9

 
cout << " a sum of " << (i*(i + 1)/2) - ((i - 8)*(i - 7)/2) << endl;
If you use that formula, you have to increment after the if statement, and change the condition of the if statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    int i = 1;
    while (i <= 104)
    {
        cout << i << " ";
    if ((i) % 8 == 0)
    {
        cout << " a sum of " << (i*(i + 1)/2) - ((i - 8)*(i - 7)/2) << endl;
    }
    i ++;
    }
}
Topic archived. No new replies allowed.