CSC242 Introduction to Programming Concepts Week Two Assignment
First Name
Last Name
ID#
Email Address
Table of Contents
How to submit your Assignment 2
P1 – Geometry: Area of a Regular Polygon 2
P2 – Table of Squares and Cubes 3
P3 – Diamond Printing Program 3
P4 – Common Characters of two Strings 4
P5 – Factorial 5
P6 – Calculate e 5
How to submit your Assignment
After filling all the parts in this file, please follow the following steps.
1) Add your name and ID to the first page.
2) Save the file in the original format (Docx or Doc)
(please do not convert to other file formats e.g. PDF, ZIP, RAR, …).
3) Rename the file as
CSC242 – HW2 - ID – YOUR Last Name - YOUR First Name.docx
Example: CSC242 – HW XXXXXXXXXXSmith - John.docx
4) Upload the file and submit it (only using Blackboard)
P1 – Geometry: Area of a Regular Polygon
A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is
Here, s is the length of a side.
Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area. Here is a sample run:
Your C++ code for this problem
-- Copy and paste your code here
Run the code and insert the result in the following box.
The run result
Copy and paste the result here (e.g. the screenshot of the result you get by running the code).
P2 – Table of Squares and Cubes
Use the loops to write a program that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format, as shown below.
Your C++ code for this problem
-- Copy and paste your code here
Run the code and insert the result in the following box.
The run result
Copy and paste the result here (e.g. the screenshot of the result you get by running the code).
P3 – Diamond Printing Program
Use the loops to write an application that prints the following diamond shape.
Your C++ code for this problem
-- Copy and paste your code here
Run the code and insert the result in the following box.
The run result
Copy and paste the result here (e.g. the screenshot of the result you get by running the code).
P4 – Common Characters of two Strings
Write a program that prompts the user to enter two strings and then find the common characters of the two strings and displays them.
Example:
string1 = “Hello”
string1 = “pilot”
Common Characters = lo
Your C++ code for this problem
-- Copy and paste your code here
Run the code and insert the result in the following box.
The run result
Copy and paste the result here (e.g. the screenshot of the result you get by running the code).
P5 – Factorial
The factorial of a nonnegative integer n is written as 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.
Write a program that reads a nonnegative integer and computes and prints its factorial.
Your C++ code for this problem
-- Copy and paste your code here
Run the code and insert the result in the following box.
The run result
Copy and paste the result here (e.g. the screenshot of the result you get by running the code).
P6 – Calculate e
Use the factorial program in the previous problem and extend it to write a program that estimates the value of the mathematical constant e by using the following formula. Allow the user to enter the number of terms to calculate.
Your C++ code for this problem
-- Copy and paste your code here
Run the code and insert the result in the following box.
The run result
Copy and paste the result here (e.g. the screenshot of the result you get by running the code).
The end