Assignment 2
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Assignment 2
Note: Late homework assignments will not be accepted, unless you have a valid written excuse (medical, etc.). You must do this assignment alone. No team work or "talking with your friends" will be accepted. No copying from the Internet. Cheating means zero. Here are a few extra instructions: Give meaningful names to your variables so we can easily know what each variable is used for in your program. Put comments in your code (in English!) to explain WHAT your code is doing and also to explain HOW your program is doing it. Make sure all your code is properly indented (formatted). Your code should be beautiful to read. Write programs in C++ to complete the following tasks:
Task 1
Accept a positive interger number from keyboard and display the n terms of natural number and their sum. Here is the author's sample output:
--- Task 1 ---
Please input a number of terms: 5
The natural numbers up to 5 terms are:
1 2 3 4 5
The sum of the natural numbers is: 15
--- Task 1 ---
Please input a number of terms: 8
The natural numbers up to 8 terms are:
1 2 3 4 5 6 7 8
The sum of the natural numbers is: 36
Task 2
Find the perfect numbers between 1 and 1000. Note: In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. Perfect Number example:
Suppose original number : 6
Divisor of 6 : 1 , 2 , 3
Sum of divisor : 1 + 2 + 3 = 6
Sum == original number
Task 3
Input an interger number and check whether it is prime or not. Here is the author's sample output:
--- Task 3 ---
Input a number to check prime or not: 21
The number you entered is not a prime number.
--- Task 3 --- Input a number to check prime or not: 23 The entered number is a prime number. |
Task 4
Rewrite the following program using a switch statement (and with no if statement). Assume a student ʼs score is a non-negative integer from 0 to 100.
#include
using namespace std; int main() { int score; cout << "Enter score:" << endl; cin >> score; if(score >= 90) cout << "Grade A"; else if(score >= 80 ) cout << "Grade B"; else if(score >= 70 ) cout << "Grade C"; else if(score >= 60 ) cout << "Grade D"; else cout << "Fail the exam"; |
return 0 ;
}
Task 5
Enter the uppercase alphabet you want to print in the last row. Suppose input E, your program should output as below:
Enter the uppercase character you want to print in the last row: E
A
B B
C C C
D D D D
E E E E E
Suppose input H, your program should output as below:
Enter the uppercase character you want to print in the last row: H
A B B C C C D D D D E E E E E F F F F F F G G G G G G G H H H H H H H H |
Task 6
Display an inverted pyramid according use input number. Suppose input 5 , your program should output as below:
Enter number of rows: 5
* * * * * * * * * * * * * * * * * * * * * * * * * |
Suppose input 8 , your program should output as below:
Enter number of rows: 8
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
Task 7
Ask the user to enter a positive integer number, to find its reverse integer number and then check whether reverse is equal to its original or not, then display the result on the screen. Suppose input 123 , your program should output as below:
Enter a number : 123
The reverse number is 321 Reverse is not equal to original |
Suppose input 123321 , your program should output as below:
Enter a number : 123321
The reverse number is 123321
Reverse is equal to original
Submission
You should create a single cpp file FOR EACH TASK following the format
Assignment2_q0123456789_task_x.cpp. You should replace q0123456789 with your student ID number and x with 1, 2, 3, 4, 5, 6, 7 respectively. And compress all these cpp files into a zip file and rename it
Assignment2_q0123456789.zip and upload to iSpace before deadline. (replace q0123456789 with your student ID number) Deadline will be announced on iSpace.
2023-10-20