2. (Estimates of Pi) an Approximate Value of Pi Can Be Calculated Using the Series Given Below
Total Page:16
File Type:pdf, Size:1020Kb
CMPT 201 Fall 2017 Homework 4 Due: Friday, October 27 at 11AM
Please print your name and assignment number and submit a hardcopy (printout) of your solution. Your solution to each problem should include the C++ source code and the screenshot of the a test run. A solution without a test run will have points deducted.
1. (Characters for the ASCII Codes) Write a program that uses a for loop to display the characters for the ASCII codes 32 through 127. Display 16 characters on each line.
2. (Estimates of Pi) An approximate value of pi can be calculated using the series given below:
pi = 4 [1 – 1/3 + 1/5 – 1/7 + 1/9 ... + ((-1)n)/(2n + 1) ]
Write a program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of terms in the approximation of the value of pi and outputs the approximation.
3. (Prime Numbers) Write a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, ...). One way to solve this problem is to use a doubly nested loop. The outer loop can iterate from 3 to 100 while the inner loop checks to see if the counter value for the outer loop is prime. One way to see if number n is prime is to loop from 2 to n – 1 and if any of these numbers evenly divides n, then n must be prime. (Extra credit will be given if a more efficient algorithm is developed.)