[SOLVED] 02393 C++ Programming Exercises Assignment 2

30.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

Rate this product

Gaussian Sum.

Write a program that computes the sum 1 + 2 + . . . + n for a given positive

integer n. For example, for n = 100, the result is 5050.

Another sum.

Write a program that computes the sum of all even integers between 0 and n.

For instance, for n = 6, the result is 0 + 2 + 4 + 6 = 12.

Prime Factorization.

Write a program that computes the prime factorization of a given posi

tive integer. For instance, the factorization of 60 is 2 ∗ 2 ∗ 3 ∗ 5.

Hints:

  • In C++, the modulus function % gives the remainder of integer division, i.e., x is divisible

by y if and only if x%y == 0.

  • Given the number n to factorize, iterate through all the numbers i = 2, 3, 4, 5, . . . and check

whether i divides n. If so, print out “i ∗ ” and continue to check the factorization of n/i.

Stop when n cannot be further factorised.

In order to check with CodeJudge, please ensure that (i) the factors are printed in ascending

order, (ii) between two factors print a space, an asterisk (∗), and another space, (iii) and at the

end there is a newline (see example above).

Approximating π. Compute an approximation of π using Leibniz’ formula:

π

4

=

∞

X

i=0

(

−

1)i

2

i

+ 1

= 1 −

1

3

+

1

5

−

1

7

+

1

9

−

1

11

+ . . .

To that end write a function with header double pi(int n) that computes the fifirst n terms

of the infifinite summation (and then multiplies by 4). For instance for n = 1 we get the bad

approximation 4, and with increasing n, the approximation gets better.

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.